Debian Codegraph @

third_party/rust/warp/examples/headers.rs

raw
1 #![deny(warnings)]
2 use std::net::SocketAddr;
3 use warp::Filter;
4
5 /// Create a server that requires header conditions:
6 ///
7 /// - `Host` is a `SocketAddr`
8 /// - `Accept` is exactly `*/*`
9 ///
10 /// If these conditions don't match, a 404 is returned.
11 #[tokio::main]
12 async fn main() {
13 pretty_env_logger::init();
14
15 // For this example, we assume no DNS was used,
16 // so the Host header should be an address.
17 let host = warp::header::<SocketAddr>("host");
18
19 // Match when we get `accept: */*` exactly.
20 let accept_stars = warp::header::exact("accept", "*/*");
21
22 let routes = host
23 .and(accept_stars)
24 .map(|addr| format!("accepting stars on {}", addr));
25
26 warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
27 }