#hyper #run-time #integration #tcp-listener #async #asynk

asynk-hyper

Hyper integration with asynk runtime

2 releases

0.1.1 Nov 6, 2024
0.1.0 Nov 6, 2024

#6 in #tcp-listener

Download history 234/week @ 2024-11-04 5/week @ 2024-11-11 17/week @ 2024-11-18

256 downloads per month

MIT/Apache

33KB
673 lines

asynk-hyper

Hyper integration with asynk runtime

Example

use asynk_hyper::TcpListener;
use futures::StreamExt;
use http_body_util::Full;
use hyper::{body::Bytes, server::conn::http1, service::service_fn, Request, Response};
use std::convert::Infallible;

const SERVER_SOCK_ADDR: &str = "127.0.0.1:8040";

fn main() {
    asynk::builder().build().unwrap();
    asynk::block_on(server()).unwrap();
}

async fn server() {
    let addr = SERVER_SOCK_ADDR.parse().unwrap();

    let listener = TcpListener::bind(addr).unwrap();
    let mut accept = listener.accept();

    while let Some(res) = accept.next().await {
        // Spawn new task for the connection
        asynk::spawn(async move {
            // Accept the connection
            let (stream, _) = res.unwrap();

            if let Err(e) = http1::Builder::new()
                .serve_connection(stream, service_fn(hello))
                .await
            {
                eprintln!("error serving connection: {:?}", e);
            }
        });
    }
}

async fn hello(_: Request<impl hyper::body::Body>) -> Result<Response<Full<Bytes>>, Infallible> {
    Ok(Response::new(Full::new(Bytes::from(
        "<h1>Hello, World!</h1>",
    ))))
}

Dependencies

~7–16MB
~206K SLoC