From fb657e033613f63a6969d3bb94231e2a9e68c126 Mon Sep 17 00:00:00 2001 From: Raphael C Date: Wed, 12 Oct 2022 13:00:30 +0200 Subject: [PATCH] feat(limitation): limiter with scope function --- actix-limitation/src/lib.rs | 8 ++------ actix-limitation/src/middleware.rs | 7 +++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/actix-limitation/src/lib.rs b/actix-limitation/src/lib.rs index 58a3fa5b3..de5e5b7d2 100644 --- a/actix-limitation/src/lib.rs +++ b/actix-limitation/src/lib.rs @@ -232,15 +232,11 @@ mod tests { let app = actix_web::test::init_service( actix_web::App::new() - .wrap(RateLimiter { - scope: Some("default"), - }) + .wrap(RateLimiter::scoped("default")) .app_data(limiters.clone()) .service( web::scope("/scoped") - .wrap(RateLimiter { - scope: Some("scoped"), - }) + .wrap(RateLimiter::scoped("scoped")) .service(index), ) .service(index), diff --git a/actix-limitation/src/middleware.rs b/actix-limitation/src/middleware.rs index c2fdc71c3..c968204d0 100644 --- a/actix-limitation/src/middleware.rs +++ b/actix-limitation/src/middleware.rs @@ -22,6 +22,13 @@ pub struct RateLimiter { pub scope: Option<&'static str>, } +impl RateLimiter { + /// Construct the rate limiter with a scope + pub fn scoped(scope: &'static str) -> Self { + RateLimiter { scope: Some(scope) } + } +} + impl Transform for RateLimiter where S: Service, Error = Error> + 'static,