Skip to content

Commit

Permalink
Update axum example to 0.8 (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
Threated authored Feb 14, 2025
1 parent 65765f3 commit 7964685
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
8 changes: 4 additions & 4 deletions examples/axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ edition = "2021"
publish = false

[dependencies]
axum = "0.7"
hyper = { version = "1", features = ["full"] }
hyper-util = { version = "0.1.2", features = ["full"] }
axum = "0.8"
hyper = "1"
hyper-util = { version = "0.1.2", features = ["client-legacy"] }
http-body-util = "0.1"
turmoil = { path = "../.." }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tokio = "1"
tower = { version = "0.4", features = ["util"] }
tower = { version = "0.5", features = ["util"] }
pin-project-lite = "0.2"
47 changes: 22 additions & 25 deletions examples/axum/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use axum::{body::Body, extract::Path, http::Request, routing::get, Router};
use axum::{body::Body, extract::Path, http::Request, routing::get, serve::Listener, Router};
use http_body_util::BodyExt as _;
use hyper_util::{client::legacy::Client, rt::TokioExecutor};
use std::net::{IpAddr, Ipv4Addr};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use tracing::{info_span, Instrument};
use turmoil::{net, Builder};

Expand All @@ -19,36 +19,17 @@ fn main() {
let mut sim = Builder::new().build();

let router = Router::new().route(
"/greet/:name",
"/greet/{name}",
get(|Path(name): Path<String>| async move { format!("Hello {name}!") }),
);

sim.host("server", move || {
let router = router.clone();
async move {
let listener = net::TcpListener::bind(addr).await?;
loop {
let (tcp_stream, _remote_addr) = listener.accept().await?;
let tcp_stream = hyper_util::rt::TokioIo::new(tcp_stream);

let hyper_service = hyper_util::service::TowerToHyperService::new(router.clone());

let result = hyper_util::server::conn::auto::Builder::new(
hyper_util::rt::TokioExecutor::new(),
)
.serve_connection_with_upgrades(tcp_stream, hyper_service)
.await;
if result.is_err() {
// This error only appears when the client doesn't send a request and
// terminate the connection.
//
// If client sends one request then terminate connection whenever, it doesn't
// appear.
break;
}
}

Ok(())
axum::serve(TurmoilListener(listener), router)
.await
.map_err(Into::into)
}
.instrument(info_span!("server"))
});
Expand Down Expand Up @@ -76,6 +57,22 @@ fn main() {
sim.run().unwrap();
}

struct TurmoilListener(net::TcpListener);

impl Listener for TurmoilListener {
type Io = net::TcpStream;

type Addr = SocketAddr;

fn accept(&mut self) -> impl std::future::Future<Output = (Self::Io, Self::Addr)> + Send {
async move { self.0.accept().await.unwrap() }
}

fn local_addr(&self) -> tokio::io::Result<Self::Addr> {
self.0.local_addr()
}
}

mod connector {
use hyper::Uri;
use pin_project_lite::pin_project;
Expand Down

0 comments on commit 7964685

Please sign in to comment.