Crates.io | tower-hyper-http-body-compat |
lib.rs | tower-hyper-http-body-compat |
version | 0.2.0 |
source | src |
created_at | 2023-02-26 14:29:52.182522 |
updated_at | 2023-07-15 13:33:03.610465 |
description | Adapters between hyper 0.14-1.0, http-body 0.4-1.0, and tower-service 0.3. |
homepage | https://github.com/davidpdrsn/tower-hyper-http-body-compat |
repository | https://github.com/davidpdrsn/tower-hyper-http-body-compat |
max_upload_size | |
id | 795136 |
size | 45,848 |
tower-hyper-http-body-compat
provides adapters between hyper 0.14-1.0,
http-body 0.4-1.0, and tower-service 0.3.
More information about this crate can be found in the crate documentation.
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);
}
});
}
}