I've tried actix-limitation, but i think that is not the way to implement this. I've tried bursting my endpoint with python script, but all requests were sucessfull and no 425 HTTP errors were returned, as i was expecting.
pub async fn start_server() -> anyhow::Result<()> {
println!("Backend server launched");
let limiter = web::Data::new(
Limiter::builder("redis://127.0.0.1")
.key_by(|req: &ServiceRequest| {
req.get_session()
.get(&"session-id")
.unwrap_or_else(|_| req.cookie(&"rate-api-id").map(|c| c.to_string()))
})
.limit(1)
.period(Duration::from_secs(10)) // 60 minutes
.build()?,
);
HttpServer::new(move || {
let cors = Cors::default()
.allowed_origin("http://localhost:3000")
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
.allowed_header(http::header::CONTENT_TYPE);
App::new()
.wrap(cors)
.wrap(Logger::default()) // Logger para monitorar requisições
.wrap(RateLimiter::default())
.app_data(limiter.clone())
.service(return_daily_events)
.service(return_match_page)
})
.bind(("127.0.0.1", 3001))?
.run()
.await?;
Ok(())
}