|
1 | 1 | #![deny(warnings)]
|
2 | 2 |
|
| 3 | +use std::convert::Infallible; |
| 4 | + |
3 | 5 | use hyper::{Body, Request, Response, Server};
|
4 | 6 | use hyper::service::{make_service_fn, service_fn};
|
5 | 7 |
|
6 |
| -async fn hello(_: Request<Body>) -> Result<Response<Body>, hyper::Error> { |
| 8 | +async fn hello(_: Request<Body>) -> Result<Response<Body>, Infallible> { |
7 | 9 | Ok(Response::new(Body::from("Hello World!")))
|
8 | 10 | }
|
9 | 11 |
|
10 | 12 | #[tokio::main]
|
11 | 13 | pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
12 | 14 | pretty_env_logger::init();
|
13 | 15 |
|
| 16 | + // For every connection, we must make a `Service` to handle all |
| 17 | + // incoming HTTP requests on said connection. |
| 18 | + let make_svc = make_service_fn(|_conn| { |
| 19 | + // This is the `Service` that will handle the connection. |
| 20 | + // `service_fn` is a helper to convert a function that |
| 21 | + // returns a Response into a `Service`. |
| 22 | + async { |
| 23 | + Ok::<_, Infallible>(service_fn(hello)) |
| 24 | + } |
| 25 | + }); |
| 26 | + |
14 | 27 | let addr = ([127, 0, 0, 1], 3000).into();
|
15 | 28 |
|
16 |
| - let server = Server::bind(&addr) |
17 |
| - .serve(make_service_fn(|_| { |
18 |
| - // This is the `Service` that will handle the connection. |
19 |
| - // `service_fn` is a helper to convert a function that |
20 |
| - // returns a Response into a `Service`. |
21 |
| - async { |
22 |
| - Ok::<_, hyper::Error>(service_fn(hello)) |
23 |
| - } |
24 |
| - })); |
| 29 | + let server = Server::bind(&addr).serve(make_svc); |
25 | 30 |
|
26 | 31 | println!("Listening on http://{}", addr);
|
27 | 32 |
|
|
0 commit comments