--- /dev/null
+[package]
+name = "hyper-server"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+futures = "0.3"
+hyper = { version = "0.14", features = ["server", "runtime", "http1"] }
+tokio = { version = "1", features = ["full"] }
--- /dev/null
+use std::convert::Infallible;
+use std::net::SocketAddr;
+
+use hyper::service::{make_service_fn, service_fn};
+use hyper::{Body, Request, Response, Server, Method, StatusCode};
+
+async fn service(req: Request<Body>) -> Result<Response<Body>, Infallible> {
+ let mut response = Response::new(Body::empty());
+
+ match (req.method(), req.uri().path()) {
+ (&Method::GET, "/") => {
+ *response.body_mut() = Body::from("Try POSTing data to /echo");
+ },
+ (&Method::POST, "/echo") => {
+ *response.body_mut() = req.into_body();
+ },
+ _ => {
+ *response.status_mut() = StatusCode::NOT_FOUND;
+ }
+ }
+
+ Ok(response)
+}
+
+#[tokio::main]
+async fn main() {
+ // We'll bind to 127.0.0.1:3000
+ let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
+ let make_svc = make_service_fn(|_conn| async {
+ Ok::<_, Infallible>(service_fn(service))
+ });
+ let server = Server::bind(&addr).serve(make_svc);
+ let graceful = server.with_graceful_shutdown(async {
+ tokio::signal::ctrl_c()
+ .await
+ .expect("failed to install CTRL+C signal handler");
+ });
+
+ if let Err(e) = graceful.await {
+ eprintln!("server error: {}", e);
+ }
+}