Module header_from_str_config
Expand description
Extract a header config from a request or response and insert it into the Extensions
of its Context
.
§Example
use rama_http::layer::header_from_str_config::HeaderFromStrConfigLayer;
use rama_http::service::web::{WebService};
use rama_http::{Body, Request, StatusCode, HeaderName};
use rama_core::{Context, Service, Layer};
use serde::Deserialize;
#[tokio::main]
async fn main() {
let service = HeaderFromStrConfigLayer::<String>::required(HeaderName::from_static("x-proxy-labels"))
.with_repeat(true)
.layer(WebService::default()
.get("/", |ctx: Context<()>| async move {
// For production-like code you should prefer a custom type
// to avoid possible conflicts. Ideally these are also as
// cheap as possible to allocate.
let labels: &Vec<String> = ctx.get().unwrap();
assert_eq!("a+b+c", labels.join("+"));
}),
);
let request = Request::builder()
.header("x-proxy-labels", "a, b")
.header("x-proxy-labels", "c")
.body(Body::empty())
.unwrap();
let resp = service.serve(Context::default(), request).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
Structs§
- Layer which extracts a header CSv config for the given HeaderName from a request or response and inserts it into the
Extensions
of that object. - A
Service
which extracts a header CSV config from a request or response and inserts it into theExtensions
of that object.