Skip to content

Commit

Permalink
docs(examples): add more comments to hello server example
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Aug 30, 2019
1 parent eee2a72 commit 0331219
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
#![deny(warnings)]

use std::convert::Infallible;

use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};

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

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

// For every connection, we must make a `Service` to handle all
// incoming HTTP requests on said connection.
let make_svc = make_service_fn(|_conn| {
// This is the `Service` that will handle the connection.
// `service_fn` is a helper to convert a function that
// returns a Response into a `Service`.
async {
Ok::<_, Infallible>(service_fn(hello))
}
});

let addr = ([127, 0, 0, 1], 3000).into();

let server = Server::bind(&addr)
.serve(make_service_fn(|_| {
// This is the `Service` that will handle the connection.
// `service_fn` is a helper to convert a function that
// returns a Response into a `Service`.
async {
Ok::<_, hyper::Error>(service_fn(hello))
}
}));
let server = Server::bind(&addr).serve(make_svc);

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

Expand Down

0 comments on commit 0331219

Please sign in to comment.