1Convert this router into a [`MakeService`], that will store `C`'s 2associated `ConnectInfo` in a request extension such that [`ConnectInfo`] 3can extract it. 4 5This enables extracting things like the client's remote address. 6 7Extracting [`std::net::SocketAddr`] is supported out of the box: 8 9```rust 10use axum::{ 11 extract::ConnectInfo, 12 routing::get, 13 Router, 14}; 15use std::net::SocketAddr; 16 17let app = Router::new().route("/", get(handler)); 18 19async fn handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> String { 20 format!("Hello {}", addr) 21} 22 23# async { 24axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) 25 .serve( 26 app.into_make_service_with_connect_info::<SocketAddr>() 27 ) 28 .await 29 .expect("server failed"); 30# }; 31``` 32 33You can implement custom a [`Connected`] like so: 34 35```rust 36use axum::{ 37 extract::connect_info::{ConnectInfo, Connected}, 38 routing::get, 39 Router, 40}; 41use hyper::server::conn::AddrStream; 42 43let app = Router::new().route("/", get(handler)); 44 45async fn handler( 46 ConnectInfo(my_connect_info): ConnectInfo<MyConnectInfo>, 47) -> String { 48 format!("Hello {:?}", my_connect_info) 49} 50 51#[derive(Clone, Debug)] 52struct MyConnectInfo { 53 // ... 54} 55 56impl Connected<&AddrStream> for MyConnectInfo { 57 fn connect_info(target: &AddrStream) -> Self { 58 MyConnectInfo { 59 // ... 60 } 61 } 62} 63 64# async { 65axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) 66 .serve( 67 app.into_make_service_with_connect_info::<MyConnectInfo>() 68 ) 69 .await 70 .expect("server failed"); 71# }; 72``` 73 74See the [unix domain socket example][uds] for an example of how to use 75this to collect UDS connection info. 76 77[`MakeService`]: tower::make::MakeService 78[`Connected`]: crate::extract::connect_info::Connected 79[`ConnectInfo`]: crate::extract::connect_info::ConnectInfo 80[uds]: https://github.com/tokio-rs/axum/blob/main/examples/unix-domain-socket/src/main.rs 81