https://github.com/realaravinth/actix-optional-middleware
Conditionally load actix-web middleware
https://github.com/realaravinth/actix-optional-middleware
actix-web actix-web-middleware
Last synced: 3 months ago
JSON representation
Conditionally load actix-web middleware
- Host: GitHub
- URL: https://github.com/realaravinth/actix-optional-middleware
- Owner: realaravinth
- License: apache-2.0
- Created: 2021-07-10T12:27:30.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-12-17T06:12:23.000Z (over 3 years ago)
- Last Synced: 2025-04-13T16:16:06.443Z (3 months ago)
- Topics: actix-web, actix-web-middleware
- Language: Rust
- Homepage: https://realaravinth.github.io/actix-optional-middleware/actix_optional_middleware/
- Size: 2.04 MB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
Actix Optional Middleware
Conditionally load middleware
[](https://realaravinth.github.io/actix-optional-middleware/actix_optional_middleware/)
[](https://github.com/realaravinth/actix-optional-middleware/actions/workflows/linux.yml)
[](https://deps.rs/repo/github/realaravinth/actix-optional-middleware)[](https://codecov.io/gh/realaravinth/actix-optional-middleware)
This library supports conditional loading of an Actix Web middleware.
When conditions are not met, a dummy middleware that simply forwards
requests are substituted.## Usage
Add this to your `Cargo.toml`:
```toml
actix-optional-middleware = { version = "0.1", git = "https://github.com/realaravinth/actix-optional-middleware" }
```## Example
```rust
use std::rc::Rc;use actix_optional_middleware::{Group, Dummy};
use actix_web::dev::{AnyBody, Service, ServiceRequest,
ServiceResponse, Transform};
use actix_web::middleware::DefaultHeaders;
use actix_web::{web, App, Error, HttpServer, Responder, get};#[get("/test", wrap = "get_group_middleware()")]
async fn h1() -> impl Responder {
"Handler 1"
}// flip this value to see dummy in action
const ACTIVE: bool = true;fn get_group_middleware() -> Group
where
S: Service, Error = Error>
+ 'static,
{
if ACTIVE {
Group::Real(Rc::new(DefaultHeaders::new()
.header("Permissions-Policy", "interest-cohort=()"
)))
} else {
Group::default()
}
}
```