1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. //! An Actix web middleware for rate limiting requests using a fixed window counter keyed on a //! header. //! //! # Usage //! //! Add `limitation-actix-middleware` to your `Cargo.toml`: //! //! ```toml //! [dependencies] //! limitation-actix-middleware = "0.1.1" //! ``` //! //! ## Quick Example //! //! The [`RateLimiter`] middleware is the primary type which is intended to be inserted in an Actix //! web app's middleware chain. The middleware requires 2 `Data` types to be present: //! //! 1. A `HeaderName` which is the header to use as the rate limiter key //! 2. A [`Limiter`] which performs the rate limiting and manages persistence //! //! ```no_run //! use actix_web::{http::header::HeaderName, web, App, HttpResponse}; //! use limitation_actix_middleware::{Limiter, RateLimiter}; //! //! // Choose a header to use for rate limit tracking //! let header = web::Data::new(HeaderName::from_static("authorization")); //! // Build a `Limiter` which will be used by the middleware //! let limiter = web::Data::new(Limiter::build("redis://127.0.0.1/").finish()?); //! //! let app = App::new() //! // Register the header as application data //! .register_data(header.clone()) //! // Register the Limiter as application data //! .register_data(limiter.clone()) //! // Insert the RateLimter middleware //! .wrap(RateLimiter) //! .service( //! web::resource("/test") //! .route(web::get().to(|| HttpResponse::Ok())) //! .route(web::head().to(|| HttpResponse::MethodNotAllowed())) //! ); //! # Ok::<(), limitation_actix_middleware::Error>(()) //! ``` //! //! [`Limiter`]: struct.Limiter.html //! [`RateLimiter`]: struct.RateLimiter.html //! //! # Examples //! //! This crate ships with an example program called [catchall] which can be run from the sources //! with: //! //! ```console //! $ cargo run --example catchall //! ``` //! //! [catchall]: //! https://github.com/fnichol/limitation/blob/master/limitation-actix-middleware/examples/catchall.rs #![doc(html_root_url = "https://docs.rs/limitation-actix-middleware/0.1.1")] #![deny(missing_docs)] mod rate_limiter; pub use rate_limiter::RateLimiter; // re-export Limitation types pub use limitation::{Builder, Error, Limiter, Status};