A middleware for actix-web that provides rate-limiting backed by governor.
```rust use actixgovernor::{Governor, GovernorConfigBuilder}; use actixweb::{web, App, HttpServer, Responder};
async fn index() -> impl Responder { "Hello world!" }
async fn main() -> std::io::Result<()> { // Allow bursts with up to five requests per IP address // and replenishes one element every two seconds let governorconf = GovernorConfigBuilder::default() .persecond(2) .burst_size(5) .finish() .unwrap();
HttpServer::new(move || {
App::new()
// Enable Governor middleware
.wrap(Governor::new(&governor_conf))
// Route hello world service
.route("/", web::get().to(index))
})
.bind("127.0.0.1:8080")?
.run()
.await
} ```
Cargo.toml
:toml
[dependencies]
actix-governor = "0.3"