http1/
http1.rs

1use asynk_hyper::TcpListener;
2use futures::StreamExt;
3use http_body_util::Full;
4use hyper::{body::Bytes, server::conn::http1, service::service_fn, Request, Response};
5use std::convert::Infallible;
6
7const SERVER_SOCK_ADDR: &str = "127.0.0.1:8040";
8
9fn main() {
10    asynk::builder().build().unwrap();
11    asynk::block_on(server()).unwrap();
12}
13
14async fn server() {
15    let addr = SERVER_SOCK_ADDR.parse().unwrap();
16
17    let listener = TcpListener::bind(addr).unwrap();
18    let mut accept = listener.accept().unwrap();
19
20    while let Some(res) = accept.next().await {
21        // Spawn new task for the connection
22        asynk::spawn(async move {
23            // Accept the connection
24            let (stream, _) = res.unwrap();
25
26            if let Err(e) = http1::Builder::new()
27                .serve_connection(stream, service_fn(hello))
28                .await
29            {
30                eprintln!("error serving connection: {:?}", e);
31            }
32        });
33    }
34}
35
36async fn hello(req: Request<impl hyper::body::Body>) -> Result<Response<Full<Bytes>>, Infallible> {
37    println!("got request: {:#?}", req.headers());
38    Ok(Response::new(Full::new(Bytes::from(
39        "<h1>Hello, World!</h1>",
40    ))))
41}