#header #tower #tower-http #http #tower-service #http-response

tunnelbana-headers

Parse cloudflare-style _headers files and add them to your HTTP servers

2 releases

0.1.1 Nov 11, 2024
0.1.0 Nov 11, 2024

#1985 in Web programming

Download history 271/week @ 2024-11-11 23/week @ 2024-11-18 19/week @ 2024-11-25 34/week @ 2024-12-02 14/week @ 2024-12-09

104 downloads per month
Used in tunnelbana

MIT/Apache

11KB
183 lines

tunnelbana-headers

Generate headers from _headers text files and serve them with tower.

https://docs.rs/tunnelbana-headers


lib.rs:

tunnelbana-headers

A tower middleware to add headers to specific routes, or route groups.

Part of the tunnelbana project.

Example

use tower_http::services::ServeDir;
use tower::{ServiceBuilder, ServiceExt};
use http::Response;
use tunnelbana_headers::HeadersLayer;

let config = r#"
/example
 X-Example-Header: example.org
/subpath/{other}
 X-Header-One: h1
 X-Header-Two: h2
/wildcard/{*wildcard}
 X-Header-A: ha
 X-Header-B: hb
"#;
let headers = tunnelbana_headers::parse(config).expect("Failed to parse headers");
let headers_mw = HeadersLayer::new(headers).expect("Failed to route headers");
let serve_dir = ServeDir::new("/var/www/html").append_index_html_on_directories(true);
let service = ServiceBuilder::new()
   .layer(headers_mw)
   .service(serve_dir);

Dependencies

~1.6–2.3MB
~41K SLoC