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

tunnelbana-headers

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

3 releases

0.1.2 Jan 24, 2025
0.1.1 Nov 11, 2024
0.1.0 Nov 11, 2024

#1948 in Web programming

Download history 257/week @ 2024-11-10 33/week @ 2024-11-17 16/week @ 2024-11-24 39/week @ 2024-12-01 15/week @ 2024-12-08 5/week @ 2024-12-15 14/week @ 2024-12-22 2/week @ 2024-12-29 90/week @ 2025-01-19 29/week @ 2025-01-26 7/week @ 2025-02-02

126 downloads per month
Used in tunnelbana

MIT/Apache

11KB
203 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
~40K SLoC