Crate tower_default_headers

Source
Expand description

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));

let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();

Structsยง

DefaultHeadersLayer
middleware to set default HTTP response headers