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

tower-default-headers

tower-compatible middleware to set default HTTP response headers

1 unstable release

0.1.1 Nov 25, 2021

#1496 in HTTP server

Download history 5/week @ 2024-07-27 1/week @ 2024-08-03 5/week @ 2024-08-10 7/week @ 2024-08-17 43/week @ 2024-08-24 13/week @ 2024-08-31 10/week @ 2024-09-07 47/week @ 2024-09-14 60/week @ 2024-09-21 43/week @ 2024-09-28 8/week @ 2024-10-05 20/week @ 2024-10-12 18/week @ 2024-10-19 6/week @ 2024-10-26 33/week @ 2024-11-02

77 downloads per month

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.3–2MB
~39K SLoC