Created
August 18, 2021 16:47
-
-
Save glebpom/4cfbca947054caab5dce84aa029aac06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::{convert::Infallible, net::SocketAddr}; | |
| use hyper::{Body, Request, Response, Server}; | |
| use hyper::service::{make_service_fn, service_fn}; | |
| use hyper::header::{CONTENT_TYPE, CONTENT_LENGTH, SERVER}; | |
| const RESP: &str = "Welcome!"; | |
| async fn handle(_: Request<Body>) -> Result<Response<Body>, Infallible> { | |
| let mut resp = Response::new(RESP.into()); | |
| resp | |
| .headers_mut() | |
| .insert(SERVER, "rusthttp".parse().unwrap()); | |
| resp | |
| .headers_mut() | |
| .insert(CONTENT_TYPE, "application/json".parse().unwrap()); | |
| resp | |
| .headers_mut() | |
| .insert(CONTENT_LENGTH, RESP.len().to_string().parse().unwrap()); | |
| Ok(resp) | |
| } | |
| #[tokio::main] | |
| async fn main() { | |
| let addr = SocketAddr::from(([127, 0, 0, 1], 8080)); | |
| let make_svc = make_service_fn(|_conn| async { | |
| Ok::<_, Infallible>(service_fn(handle)) | |
| }); | |
| let server = Server::bind(&addr) | |
| .serve(make_svc); | |
| if let Err(e) = server.await { | |
| eprintln!("server error: {}", e); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment