mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-21 22:56:46 +01:00
Remove examples/listen-multiple-addrs (#2951)
This commit is contained in:
parent
20b9445347
commit
5db62e8452
2 changed files with 0 additions and 69 deletions
|
@ -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"] }
|
|
|
@ -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:#}");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue