Files
hyper/examples/hello.rs
Sean McArthur 8f4b05ae78 feat(lib): update to std::future::Future
BREAKING CHANGE: All usage of async traits (`Future`, `Stream`,
`AsyncRead`, `AsyncWrite`, etc) are updated to newer versions.
2019-07-09 15:55:22 -07:00

39 lines
976 B
Rust

#![feature(async_await)]
#![deny(warnings)]
extern crate hyper;
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() {
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
// returns a Response into a `Service`.
async {
Ok::<_, hyper::Error>(service_fn(hello))
}
}));
println!("Listening on http://{}", addr);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}
fn main() {
pretty_env_logger::init();
rt::run(serve());
}