1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-02-22 18:33:18 +01:00

feat(limitation): limiter with scope function

This commit is contained in:
Raphael C 2022-10-12 13:00:30 +02:00
parent 4006a49274
commit fb657e0336
No known key found for this signature in database
GPG Key ID: D5F5B67F7A46B506
2 changed files with 9 additions and 6 deletions

View File

@ -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),

View File

@ -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<S, B> Transform<S, ServiceRequest> for RateLimiter
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,