#http #hyper #tower #axum

tower-hyper-http-body-compat

Adapters between hyper 0.14-1.0, http-body 0.4-1.0, and tower-service 0.3

5 releases

0.1.4 Mar 21, 2023
0.1.3 Mar 13, 2023
0.1.2 Feb 26, 2023
0.1.1 Feb 26, 2023
0.1.0 Feb 26, 2023

#751 in Network programming

Download history 86/week @ 2023-02-22 71/week @ 2023-03-01 411/week @ 2023-03-08 278/week @ 2023-03-15 320/week @ 2023-03-22 119/week @ 2023-03-29 312/week @ 2023-04-05 371/week @ 2023-04-12 311/week @ 2023-04-19 335/week @ 2023-04-26 284/week @ 2023-05-03 292/week @ 2023-05-10 404/week @ 2023-05-17 234/week @ 2023-05-24 262/week @ 2023-05-31

1,230 downloads per month

MIT/Apache

29KB
534 lines

tower-hyper-http-body-compat

tower-hyper-http-body-compat provides adapters between hyper 0.14-1.0, http-body 0.4-1.0, and tower-service 0.3.

Build status Crates.io Documentation

More information about this crate can be found in the crate documentation.

Example

Running an axum Router with hyper 1.0:

use axum::{Router, routing::get};
use hyper::server::conn::http1;
use std::net::SocketAddr;
use tokio::net::TcpListener;
use tower_http::trace::TraceLayer;
use tower_hyper_http_body_compat::TowerService03HttpServiceAsHyper1HttpService;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let app = Router::new()
        .route("/", get(|| async { "Hello, World!" }))
         // we can still add regular tower middleware
         .layer(TraceLayer::new_for_http());

    // `Router` implements tower-service 0.3's `Service` trait. Convert that to something
    // that implements hyper 1.0's `Service` trait.
    let service = TowerService03HttpServiceAsHyper1HttpService::new(app);

    let addr: SocketAddr = ([127, 0, 0, 1], 8080).into();

    let mut tcp_listener = TcpListener::bind(addr).await?;
    loop {
        let (tcp_stream, _) = tcp_listener.accept().await?;
        let service = service.clone();
        tokio::task::spawn(async move {
            if let Err(http_err) = http1::Builder::new()
                    .keep_alive(true)
                    .serve_connection(tcp_stream, service)
                    .await {
                eprintln!("Error while serving HTTP connection: {}", http_err);
            }
        });
    }
}

Dependencies

~4.5–9.5MB
~159K SLoC