hadead

Crates.iohadead
lib.rshadead
version0.2.0
sourcesrc
created_at2023-10-14 13:08:07.610026
updated_at2023-11-20 10:22:56.344391
descriptionRedis Rate Limiter using wallexerr
homepagehttps://github.com/wildonion/hadead/
repositoryhttps://github.com/wildonion/hadead/
max_upload_size
id1003116
size18,007
dewo (wildonion)

documentation

README

๐Ÿ“› Hadead

Redis Rate Limiter based on wallexerr ed25519 public key as the unique identifier.

๐Ÿš€ Run in Local

cargo run --bin hadead

๐Ÿงช Test

hadead.contract is a contract data contains the wallet info.

use hadead::*;
use once_cell::sync::Lazy;

pub static HADEAD: Lazy<Config> = Lazy::new(||{

    let redis_password = "REDIS_PASSWORD".to_string();
    let redis_username = "REDIS_USERNAME".to_string();
    let redis_host = "REDIS_HOST".to_string();
    let redis_port = "REDIS_PORT".to_string();
    let chill_zone_duration_in_seconds = 5;

    let hadead_instance = hadead::Config::new(
        &redis_password,
        &redis_username,
        &redis_host,
        &redis_port,
        chill_zone_duration_in_seconds, /* default is 5 miliseconds */
    );

    hadead_instance

});

pub async fn api() -> Result<actix_web::HttpResponse, actix_web::Error>{

    let hadead = HADEAD.clone();
    println!("hadead contract info: {:?}", hadead.contract.as_ref().unwrap());

    let check_rate_limited = hadead.check(hadead.id.as_ref().unwrap()).await;
    
    let Ok(flag) = check_rate_limited else{
        
        let why = check_rate_limited.unwrap_err();
        return Ok(
            HttpResponse::NotAcceptable().json(why.to_string())
        );
    };

    if flag{

        // rate limited

        return Ok(
            HttpResponse::NotAcceptable().json("rate limited")
        );

    } else{

        // other api logic
        // ...

        return Ok(
            HttpResponse::Ok().json("json data")
        );

    }

}
Commit count: 34

cargo fmt