#redirect #tower #tower-http #http #tower-service

tunnelbana-redirects

Generate redirect lists from cloudflare-style _redirects text files and serve them with tower

2 releases

0.1.1 Nov 11, 2024
0.1.0 Nov 11, 2024

#1770 in Web programming

Download history 269/week @ 2024-11-11 23/week @ 2024-11-18 21/week @ 2024-11-25 33/week @ 2024-12-02 15/week @ 2024-12-09

106 downloads per month
Used in tunnelbana

MIT/Apache

13KB
227 lines

tunnelbana-redirects

Generate redirect lists from cloudflare-style _redirects text files and serve them with tower.

https://docs.rs/tunnelbana-redirects


lib.rs:

tunnelbana-redirects

Generate redirect lists from cloudflare-style _redirects text files and serve them with tower.

Part of the tunnelbana project.

Example

use tower_http::services::ServeDir;
use tower::{ServiceBuilder, ServiceExt};
use http::Response;
use tunnelbana_redirects::RedirectsLayer;

let config = r#"
/example https://example.com 302
/subpath/{other}/final /{other}/final/ 302
/wildcard/{*wildcard} /{wildcard}
"#;
let redirects = tunnelbana_redirects::parse(config).expect("Failed to parse redirects");
let redirects_mw = RedirectsLayer::new(redirects).expect("Failed to route redirects");
let serve_dir = ServeDir::new("/var/www/html").append_index_html_on_directories(true);
let service = ServiceBuilder::new()
   .layer(redirects_mw)
   .service(serve_dir);

Dependencies

~2.2–3MB
~55K SLoC