From b6af5af769d27712ce2ce6802d2c4015b026339b Mon Sep 17 00:00:00 2001 From: SirLynix Date: Thu, 11 Jul 2024 18:14:45 +0200 Subject: [PATCH] Add governor to rate limit requests --- Cargo.toml | 1 + src/main.rs | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 1ba34b0..7f1acfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +actix-governor = "0.5" actix-web = "4.4" base64 = "0.22" cached = { version = "0.51", features = ["async"] } diff --git a/src/main.rs b/src/main.rs index 11a4736..76bc8ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use actix_governor::{Governor, GovernorConfig, GovernorConfigBuilder}; use actix_web::{middleware, web, App, HttpServer}; use cached::TimedCache; use confy::ConfyError; @@ -69,14 +70,27 @@ async fn main() -> Result<(), std::io::Error> { fetcher, }); + let governor_conf = GovernorConfig::default(); + + let player_create_governor_conf = GovernorConfigBuilder::default() + .per_second(10) + .burst_size(1) + .finish() + .unwrap(); + HttpServer::new(move || { App::new() .wrap(middleware::Logger::default()) + .wrap(Governor::new(&governor_conf)) .app_data(data_config.clone()) .app_data(pg_pool.clone()) .service(game_version) - .service(player_create) .service(player_authenticate) + .service( + web::scope("") + .wrap(Governor::new(&player_create_governor_conf)) + .service(player_create), + ) }) .bind(bind_address)? .run()