From b3640564d1aea6802051899736f8811adfc9020d Mon Sep 17 00:00:00 2001 From: "josephlenton@gmail.com" Date: Sun, 18 Aug 2024 14:22:56 +0200 Subject: [PATCH 1/3] feat: local_addr for Serve and WithGracefulShutdown Adds methods for exposing the address the Axum servers are bound to. * add `Serve::local_addr` * add `WithGracefulShutdown::local_addr` Implements #2879 --- axum/src/serve.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/axum/src/serve.rs b/axum/src/serve.rs index e23e61096b..73ed1b9313 100644 --- a/axum/src/serve.rs +++ b/axum/src/serve.rs @@ -173,6 +173,11 @@ impl Serve { ..self } } + + /// Returns the local address this server is bound to. + pub fn local_addr(&self) -> io::Result { + self.tcp_listener.local_addr() + } } #[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))] @@ -307,6 +312,11 @@ impl WithGracefulShutdown { ..self } } + + /// Returns the local address this server is bound to. + pub fn local_addr(&self) -> io::Result { + self.tcp_listener.local_addr() + } } #[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))] From 0061d0756ab06919530ab2a6cc0b7ec36a28f0d3 Mon Sep 17 00:00:00 2001 From: "josephlenton@gmail.com" Date: Sun, 18 Aug 2024 15:30:39 +0200 Subject: [PATCH 2/3] feat: tests for local_addr --- axum/src/serve.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/axum/src/serve.rs b/axum/src/serve.rs index 73ed1b9313..e2b974cfbe 100644 --- a/axum/src/serve.rs +++ b/axum/src/serve.rs @@ -175,7 +175,7 @@ impl Serve { } /// Returns the local address this server is bound to. - pub fn local_addr(&self) -> io::Result { + pub fn local_addr(&self) -> io::Result { self.tcp_listener.local_addr() } } @@ -314,7 +314,7 @@ impl WithGracefulShutdown { } /// Returns the local address this server is bound to. - pub fn local_addr(&self) -> io::Result { + pub fn local_addr(&self) -> io::Result { self.tcp_listener.local_addr() } } @@ -554,6 +554,10 @@ mod tests { routing::get, Router, }; + use std::{ + future::pending, + net::{IpAddr, Ipv4Addr}, + }; #[allow(dead_code, unused_must_use)] async fn if_it_compiles_it_works() { @@ -617,4 +621,29 @@ mod tests { } async fn handler() {} + + #[crate::test] + async fn test_serve_local_addr() { + let router: Router = Router::new(); + let addr = "0.0.0.0:0"; + + let server = serve(TcpListener::bind(addr).await.unwrap(), router.clone()); + let address = server.local_addr().unwrap(); + + assert_eq!(address.ip(), IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))); + assert_ne!(address.port(), 0); + } + + #[crate::test] + async fn test_with_graceful_shutdown_local_addr() { + let router: Router = Router::new(); + let addr = "0.0.0.0:0"; + + let server = serve(TcpListener::bind(addr).await.unwrap(), router.clone()) + .with_graceful_shutdown(pending()); + let address = server.local_addr().unwrap(); + + assert_eq!(address.ip(), IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))); + assert_ne!(address.port(), 0); + } } From 3e0cb6a3fa116bad70a1fb7641f5b9f8d80adf96 Mon Sep 17 00:00:00 2001 From: "josephlenton@gmail.com" Date: Sat, 24 Aug 2024 09:37:18 +0200 Subject: [PATCH 3/3] chore: document change in CHANGELOG.md --- axum/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 137ad1a1d7..1a69f7f7c4 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -11,10 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **added:** `axum::serve::Serve::tcp_nodelay` and `axum::serve::WithGracefulShutdown::tcp_nodelay` ([#2653]) - **added:** `Router::has_routes` function ([#2790]) - **change:** Update tokio-tungstenite to 0.23 ([#2841]) +- **added:** `Serve::local_addr` and `WithGracefulShutdown::local_addr` functions ([#2881]) [#2653]: https://github.com/tokio-rs/axum/pull/2653 [#2790]: https://github.com/tokio-rs/axum/pull/2790 [#2841]: https://github.com/tokio-rs/axum/pull/2841 +[#2881]: https://github.com/tokio-rs/axum/pull/2881 # 0.7.5 (24. March, 2024)