aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-01-04 17:54:49 -0800
committerFuwn <[email protected]>2022-01-04 17:54:49 -0800
commit6cb4af179347eb0f4d6186dedb857bf9785a2ebb (patch)
treec1880b2ffad43118016e30f32d127bdd8912aee5
parentchore(Cargo.toml): tidy up (diff)
downloadapi-worker-6cb4af179347eb0f4d6186dedb857bf9785a2ebb.tar.xz
api-worker-6cb4af179347eb0f4d6186dedb857bf9785a2ebb.zip
feat(main.rs): ratelimit (100 requests/minute)
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs11
2 files changed, 11 insertions, 1 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 4b9e64f..8c5d5b7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,3 +24,4 @@ dotenv = "0.15.0"
actix-web = { version = "3.3.2", features = ["rustls"] }
qstring = "0.7.2"
actix-cors = "0.5.4"
+actix-ratelimit = "0.3.1"
diff --git a/src/main.rs b/src/main.rs
index 6f07c84..cd689ed 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -15,9 +15,18 @@ pub mod utils;
async fn main() -> std::io::Result<()> {
dotenv::dotenv().ok();
- actix_web::HttpServer::new(|| {
+ let store = actix_ratelimit::MemoryStore::new();
+
+ actix_web::HttpServer::new(move || {
actix_web::App::new()
.wrap(actix_cors::Cors::default().allow_any_origin())
+ .wrap(
+ actix_ratelimit::RateLimiter::new(
+ actix_ratelimit::MemoryStoreActor::from(store.clone()).start(),
+ )
+ .with_interval(std::time::Duration::from_secs(60))
+ .with_max_requests(100),
+ )
.service(routes::index)
.service(
actix_web::web::scope("/api/v1")