axumにextractでIPを取得できる機能が既にある。
https://docs.rs/axum/latest/axum/struct.Router.html#method.into_make_service_with_connect_info
use axum::{ extract::ConnectInfo, }; // axum::serve(listener, app.into_make_service_with_connect_info::<SocketAddr>()).await.unwrap(); // async fn handle_index(ConnectInfo(addr): ConnectInfo<SocketAddr>, token: CsrfToken) -> impl IntoResponse { println!("{}", addr); // }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.74s
Running `target/debug/axum`
192.168.33.1:53688
なるほど、取得できるのね^^ これは大きい!