#http-header #header #response-headers #tower-http #middleware #http #tower

tower-default-headers

tower-compatible middleware to set default HTTP response headers

1 unstable release

0.1.1 Nov 25, 2021

#1298 in HTTP server

Download history 79/week @ 2023-12-06 68/week @ 2023-12-13 67/week @ 2023-12-20 23/week @ 2023-12-27 35/week @ 2024-01-03 68/week @ 2024-01-10 56/week @ 2024-01-17 42/week @ 2024-01-24 40/week @ 2024-01-31 57/week @ 2024-02-07 67/week @ 2024-02-14 73/week @ 2024-02-21 160/week @ 2024-02-28 135/week @ 2024-03-06 129/week @ 2024-03-13 115/week @ 2024-03-20

569 downloads per month
Used in product-os-router

MIT license

10KB
133 lines

tower-default-headers-rs Status Gitlab pipeline status Crates.io docs.rs

tower-compatible middleware to set default HTTP response headers

see also


lib.rs:

When building an HTTP service, you may find that many/all of your endpoints are required to return the same set of HTTP headers, so may find this crate is a convenient way to centralise these common headers into a middleware.

This middleware will apply these default headers to any outgoing response that does not already have headers with the same name(s).

Example

use axum::{
    body::Body,
    http::header::{HeaderMap, HeaderValue, X_FRAME_OPTIONS},
    routing::{get, Router},
};
use tower_default_headers::DefaultHeadersLayer;

let mut default_headers = HeaderMap::new();
default_headers.insert(X_FRAME_OPTIONS, HeaderValue::from_static("deny"));

let app = Router::new()
    .route("/", get(|| async { "hello, world!" }))
    .layer(DefaultHeadersLayer::new(default_headers));

axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
    .serve(app.into_make_service())
    .await
    .unwrap();

Dependencies

~1.4–2.2MB
~42K SLoC