Skip to content

Commit

Permalink
Update unix-domain-socket, listen-multiple-addrs, and testing examples (
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn authored Nov 27, 2023
1 parent db344fa commit e63cc49
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 447 deletions.
2 changes: 2 additions & 0 deletions examples/listen-multiple-addrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ publish = false
[dependencies]
axum = { path = "../../axum" }
hyper = { version = "1.0.0", features = ["full"] }
hyper-util = { version = "0.1", features = ["tokio", "server-auto", "http1"] }
tokio = { version = "1", features = ["full"] }
tower = { version = "0.4", features = ["util"] }
117 changes: 51 additions & 66 deletions examples/listen-multiple-addrs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,57 @@
//! Showcases how listening on multiple addrs is possible by
//! implementing Accept for a custom struct.
//! Showcases how listening on multiple addrs is possible.
//!
//! This may be useful in cases where the platform does not
//! listen on both IPv4 and IPv6 when the IPv6 catch-all listener is used (`::`),
//! [like older versions of Windows.](https://docs.microsoft.com/en-us/windows/win32/winsock/dual-stack-sockets)
//! Showcases how listening on multiple addrs is possible by
//! implementing Accept for a custom struct.
//!
//! This may be useful in cases where the platform does not
//! listen on both IPv4 and IPv6 when the IPv6 catch-all listener is used (`::`),
//! [like older versions of Windows.](https://docs.microsoft.com/en-us/windows/win32/winsock/dual-stack-sockets)
// TODO
fn main() {
eprint!("this example has not yet been updated to hyper 1.0");
use axum::{extract::Request, routing::get, Router};
use hyper::body::Incoming;
use hyper_util::{
rt::{TokioExecutor, TokioIo},
server,
};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
use tokio::net::TcpListener;
use tower::Service;

#[tokio::main]
async fn main() {
let app: Router = Router::new().route("/", get(|| async { "Hello, World!" }));

let localhost_v4 = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 8080);
let listener_v4 = TcpListener::bind(&localhost_v4).await.unwrap();

let localhost_v6 = SocketAddr::new(Ipv6Addr::LOCALHOST.into(), 8080);
let listener_v6 = TcpListener::bind(&localhost_v6).await.unwrap();

// See https://github.com/tokio-rs/axum/blob/main/examples/serve-with-hyper/src/main.rs for
// more details about this setup
loop {
// Accept connections from `listener_v4` and `listener_v6` at the same time
let (socket, _remote_addr) = tokio::select! {
result = listener_v4.accept() => {
result.unwrap()
}
result = listener_v6.accept() => {
result.unwrap()
}
};

let tower_service = app.clone();

tokio::spawn(async move {
let socket = TokioIo::new(socket);

let hyper_service = hyper::service::service_fn(move |request: Request<Incoming>| {
tower_service.clone().call(request)
});

if let Err(err) = server::conn::auto::Builder::new(TokioExecutor::new())
.serve_connection_with_upgrades(socket, hyper_service)
.await
{
eprintln!("failed to serve connection: {err:#}");
}
});
}
}

// use axum::{routing::get, Router};
// use hyper::server::{accept::Accept, conn::AddrIncoming};
// use std::{
// net::{Ipv4Addr, Ipv6Addr, SocketAddr},
// pin::Pin,
// task::{Context, Poll},
// };

// #[tokio::main]
// async fn main() {
// let app = Router::new().route("/", get(|| async { "Hello, World!" }));

// let localhost_v4 = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 8080);
// let incoming_v4 = AddrIncoming::bind(&localhost_v4).unwrap();

// let localhost_v6 = SocketAddr::new(Ipv6Addr::LOCALHOST.into(), 8080);
// let incoming_v6 = AddrIncoming::bind(&localhost_v6).unwrap();

// let combined = CombinedIncoming {
// a: incoming_v4,
// b: incoming_v6,
// };

// hyper::Server::builder(combined)
// .serve(app.into_make_service())
// .await
// .unwrap();
// }

// struct CombinedIncoming {
// a: AddrIncoming,
// b: AddrIncoming,
// }

// impl Accept for CombinedIncoming {
// type Conn = <AddrIncoming as Accept>::Conn;
// type Error = <AddrIncoming as Accept>::Error;

// fn poll_accept(
// mut self: Pin<&mut Self>,
// cx: &mut Context<'_>,
// ) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
// if let Poll::Ready(Some(value)) = Pin::new(&mut self.a).poll_accept(cx) {
// return Poll::Ready(Some(value));
// }

// if let Poll::Ready(Some(value)) = Pin::new(&mut self.b).poll_accept(cx) {
// return Poll::Ready(Some(value));
// }

// Poll::Pending
// }
// }
1 change: 1 addition & 0 deletions examples/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ publish = false
axum = { path = "../../axum" }
http-body-util = "0.1.0"
hyper = { version = "1.0.0", features = ["full"] }
hyper-util = { version = "0.1", features = ["client", "http1", "client-legacy"] }
mime = "0.3"
serde_json = "1.0"
tokio = { version = "1.0", features = ["full"] }
Expand Down
Loading

0 comments on commit e63cc49

Please sign in to comment.