Skip to content

Commit

Permalink
Add governor to rate limit requests
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Jul 14, 2024
1 parent ec0e836 commit b6af5af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
16 changes: 15 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use actix_governor::{Governor, GovernorConfig, GovernorConfigBuilder};
use actix_web::{middleware, web, App, HttpServer};
use cached::TimedCache;
use confy::ConfyError;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b6af5af

Please sign in to comment.