add hyperrs
This commit is contained in:
31
hyperrs/src/main.rs
Normal file
31
hyperrs/src/main.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use std::convert::Infallible;
|
||||
use hyper::service::{ make_service_fn, service_fn, };
|
||||
use hyper::{ Body, Request, Response, Server, };
|
||||
|
||||
async fn hello(_: Request<Body>) -> Result<Response<Body>, Infallible> {
|
||||
Ok(Response::new(Body::from("Hello World!\n")))
|
||||
}
|
||||
|
||||
// https://github.com/hyperium/hyper/blob/master/examples/hello.rs
|
||||
#[tokio::main]
|
||||
pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
|
||||
// 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_svc);
|
||||
|
||||
println!("Listening on http://{}", addr);
|
||||
|
||||
server.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user