Debian Codegraph @

examples/demo.rs

raw symbols SCIP
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(|_: _| async move { Ok::<_, Infallible>(svc) });
26
27 let mut listenfd = ListenFd::from_env();
28 // if listenfd doesn't take a TcpListener (i.e. we're not running via
29 // the command above), we fall back to explicitly binding to a given
30 // host:port.
31 let server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() {
32 Server::from_tcp(l).unwrap()
33 } else {
34 Server::bind(&([127, 0, 0, 1], 3030).into())
35 };
36
37 server.serve(make_svc).await.unwrap();
38 }