Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mustakimali/actix-middleware-macro
A macro to generate actix-web middleware. Useful for the times when you can't be bothered to figure it out yourself for the 100th times.
https://github.com/mustakimali/actix-middleware-macro
Last synced: 9 days ago
JSON representation
A macro to generate actix-web middleware. Useful for the times when you can't be bothered to figure it out yourself for the 100th times.
- Host: GitHub
- URL: https://github.com/mustakimali/actix-middleware-macro
- Owner: mustakimali
- Created: 2024-02-28T17:57:46.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-02-28T18:22:24.000Z (10 months ago)
- Last Synced: 2024-11-06T07:13:33.168Z (about 2 months ago)
- Language: Rust
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# actix-middleware-macro
[![Build](https://github.com/mustakimali/actix-middleware-macro/actions/workflows/rust.yml/badge.svg)](https://github.com/mustakimali/actix-middleware-macro/actions/workflows/rust.yml)
[![Latest Version](https://img.shields.io/crates/v/actix-middleware-macro.svg)](https://crates.io/crates/actix-middleware-macro)
[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/actix-middleware-macro/)A macro to generate actix-web middleware. Useful for the times when you can't be bothered to figure it out yourself for the 100th time.
## Usage
```rust
use actix_web::web;use super::*;
create_middleware!(
TimingCorsHeaders,
|ctx: &MiddlewareTransform, req: ServiceRequest| {
use actix_web::http::header::{HeaderName, HeaderValue};
use chrono::Utc;let start = Utc::now();
let fut = ctx.service.call(req);
Box::pin(async move {
let mut res = fut.await?;
let duration = Utc::now() - start;
res.headers_mut().insert(
HeaderName::from_static("x-app-time-ms"),
HeaderValue::from_str(&format!("{}", duration.num_milliseconds()))?,
);Ok(res)
})
}
);#[actix_web::test]
async fn works() {
let _server = tokio::spawn(async {
actix_web::HttpServer::new(|| {
actix_web::App::new()
.default_service(web::to(|| async { actix_web::HttpResponse::Ok() }))
.wrap(timing_cors_headers_middleware::Middleware)
})
.bind("127.1:8080")
.unwrap()
.run()
.await
.unwrap();
});tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
let response = ureq::get("http://127.1:8080").call().unwrap();
assert!(response.header("x-app-time-ms").is_some());
}
```