Debian Codegraph @

third_party/rust/warp/examples/autoreload.rs

raw
1 #![deny(warnings)]
2 use hyper::server::Server;
3 use listenfd::ListenFd;
4 use std::convert::Infallible;
5 use warp::Filter;
6
7 /// You'll need to install `systemfd` and `cargo-watch`:
8 /// ```
9 /// cargo install systemfd cargo-watch
10 /// ```
11 /// And run with:
12 /// ```
13 /// systemfd --no-pid -s http::3030 -- cargo watch -x 'run --example autoreload'
14 /// ```
15 #[tokio::main]
16 async fn main() {
17 // Match any request and return hello world!
18 let routes = warp::any().map(|| "Hello, World!");
19
20 // hyper let's us build a server from a TcpListener (which will be
21 // useful shortly). Thus, we'll need to convert our `warp::Filter` into
22 // a `hyper::service::MakeService` for use with a `hyper::server::Server`.
23 let svc = warp::service(routes);
24
25 let make_svc = hyper::service::make_service_fn(|_: _| {
26 // the clone is there because not all warp filters impl Copy
27 let svc = svc.clone();
28 async move { Ok::<_, Infallible>(svc) }
29 });
30
31 let mut listenfd = ListenFd::from_env();
32 // if listenfd doesn't take a TcpListener (i.e. we're not running via
33 // the command above), we fall back to explicitly binding to a given
34 // host:port.
35 let server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() {
36 Server::from_tcp(l).unwrap()
37 } else {
38 Server::bind(&([127, 0, 0, 1], 3030).into())
39 };
40
41 server.serve(make_svc).await.unwrap();
42 }