docs(examples): Update the example hello (#1852)

This commit is contained in:
Fuyang Liu
2019-07-10 18:43:56 +02:00
committed by Sean McArthur
parent 8f4b05ae78
commit 67c4781734

View File

@@ -5,19 +5,21 @@ extern crate pretty_env_logger;
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
use hyper::rt;
async fn hello(_: Request<Body>) -> Result<Response<Body>, hyper::Error> {
Ok(Response::new(Body::from("Hello World!")))
}
async fn serve() {
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
pretty_env_logger::init();
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_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`.
async {
Ok::<_, hyper::Error>(service_fn(hello))
@@ -26,13 +28,7 @@ async fn serve() {
println!("Listening on http://{}", addr);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}
server.await?;
fn main() {
pretty_env_logger::init();
rt::run(serve());
Ok(())
}