diff options
Diffstat (limited to 'tools/hyper_hello/hyper_hello.rs')
-rw-r--r-- | tools/hyper_hello/hyper_hello.rs | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/tools/hyper_hello/hyper_hello.rs b/tools/hyper_hello/hyper_hello.rs index 9491b0750..bf786abf9 100644 --- a/tools/hyper_hello/hyper_hello.rs +++ b/tools/hyper_hello/hyper_hello.rs @@ -2,16 +2,18 @@ // Adapted from https://github.com/hyperium/hyper/blob/master/examples/hello.rs #![deny(warnings)] -extern crate hyper; -use hyper::rt::{self, Future}; -use hyper::service::service_fn_ok; -use hyper::{Body, Response, Server}; +use std::convert::Infallible; use std::env; +use std::error::Error; + +use hyper::service::{make_service_fn, service_fn}; +use hyper::{Body, Response, Server}; -static PHRASE: &[u8] = b"Hello World!"; +type Just<T> = Result<T, Infallible>; -fn main() { +#[tokio::main] +async fn main() -> Result<(), Box<dyn Error>> { let mut port: u16 = 4544; if let Some(custom_port) = env::args().nth(1) { port = custom_port.parse::<u16>().unwrap(); @@ -19,21 +21,20 @@ fn main() { let addr = ([127, 0, 0, 1], port).into(); - // new_service is run for each connection, creating a 'service' - // to handle requests for that specific connection. - let new_service = || { + // For every connection, we must make a `Service` to handle all + // incoming HTTP requests on said connection. + let new_service = make_service_fn(|_| { // This is the `Service` that will handle the connection. - // `service_fn_ok` is a helper to convert a function that + // `service_fn` is a helper to convert a function that // returns a Response into a `Service`. - service_fn_ok(|_| Response::new(Body::from(PHRASE))) - }; - - let server = Server::bind(&addr) - .tcp_nodelay(true) - .serve(new_service) - .map_err(|e| eprintln!("server error: {}", e)); - + async { + Just::Ok(service_fn(|_req| { + async { Just::Ok(Response::new(Body::from(&b"Hello World!"[..]))) } + })) + } + }); + + let server = Server::bind(&addr).tcp_nodelay(true).serve(new_service); println!("Listening on http://{}", addr); - - rt::run(server); + Ok(server.await?) } |