Added rate-limiter

This commit is contained in:
Patrick Alvin Alcala 2025-09-12 17:47:29 +08:00
parent 4ee3824d2c
commit 3124696932
3 changed files with 188 additions and 7 deletions

View file

@ -1,4 +1,6 @@
use actix_web::{App, HttpServer, web::Data};
use actix_web::{App, http, HttpServer, web::Data};
use actix_cors::Cors;
use actix_governor::{Governor, GovernorConfigBuilder};
use dotenvy::dotenv;
use sqlx::mysql::{MySqlPool, MySqlPoolOptions};
@ -23,9 +25,24 @@ async fn main() -> std::io::Result<()> {
let app_state = AppState { pool };
HttpServer::new(move || {
App::new()
.app_data(Data::new(app_state.pool.clone()))
.service(sample)
let cors = Cors::default()
.allowed_origin("localhost:4321")
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
.allowed_header(http::header::CONTENT_TYPE)
.max_age(3600);
let governor_conf = GovernorConfigBuilder::default()
.seconds_per_request(2)
.burst_size(20)
.finish()
.unwrap();
App::new()
.app_data(Data::new(app_state.pool.clone()))
.wrap(cors)
.wrap(Governor::new(&governor_conf))
.service(sample)
})
.bind(("127.0.0.1", 4320))?
.run()