Remove examples/listen-multiple-addrs (#2951)

This commit is contained in:
Jonas Platte 2024-09-30 21:23:19 +00:00 committed by GitHub
parent 20b9445347
commit 5db62e8452
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 0 additions and 69 deletions

View file

@ -1,12 +0,0 @@
[package]
name = "example-listen-multiple-addrs"
version = "0.1.0"
edition = "2021"
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.5.1", features = ["util"] }

View file

@ -1,57 +0,0 @@
//! 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)
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:#}");
}
});
}
}