Skip to content

Commit 0331219

Browse files
committed
docs(examples): add more comments to hello server example
1 parent eee2a72 commit 0331219

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

examples/hello.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
#![deny(warnings)]
22

3+
use std::convert::Infallible;
4+
35
use hyper::{Body, Request, Response, Server};
46
use hyper::service::{make_service_fn, service_fn};
57

6-
async fn hello(_: Request<Body>) -> Result<Response<Body>, hyper::Error> {
8+
async fn hello(_: Request<Body>) -> Result<Response<Body>, Infallible> {
79
Ok(Response::new(Body::from("Hello World!")))
810
}
911

1012
#[tokio::main]
1113
pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
1214
pretty_env_logger::init();
1315

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+
1427
let addr = ([127, 0, 0, 1], 3000).into();
1528

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);
2530

2631
println!("Listening on http://{}", addr);
2732

0 commit comments

Comments
 (0)