#hyper-http #hyper #tower-http #axum #tower #http #hyper-server

tower-hyper-http-body-compat

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

6 releases

0.2.0 Jul 15, 2023
0.1.4 Mar 21, 2023
0.1.2 Feb 26, 2023

#5 in #tower-http

Download history 178/week @ 2024-02-26 107/week @ 2024-03-04 132/week @ 2024-03-11 90/week @ 2024-03-18 62/week @ 2024-03-25 153/week @ 2024-04-01 207/week @ 2024-04-08 95/week @ 2024-04-15 95/week @ 2024-04-22 76/week @ 2024-04-29 80/week @ 2024-05-06 95/week @ 2024-05-13 65/week @ 2024-05-20 85/week @ 2024-05-27 99/week @ 2024-06-03 78/week @ 2024-06-10

331 downloads per month

MIT/Apache

32KB
535 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?;

        // hyper-util isn't on crates.io yet. Instead depend on it via git
        // `hyper-util = { git = "https://github.com/hyperium/hyper-util" }`
        let tcp_stream = hyper_util::rt::TokioIo::new(tcp_stream);

        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–6.5MB
~115K SLoC