remote_address/
remote_address.rs

1//! Run with `cargo run --example remote_address` command.
2//!
3//! To connect through browser, navigate to "http://localhost:3000" url.
4
5use axum::{extract::ConnectInfo, routing::get, Router};
6use std::net::SocketAddr;
7
8#[tokio::main]
9async fn main() {
10    let app = Router::new()
11        .route("/", get(handler))
12        .into_make_service_with_connect_info::<SocketAddr>();
13
14    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
15
16    hyper_serve::bind(addr).serve(app).await.unwrap();
17}
18
19async fn handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> String {
20    format!("your ip address is: {}", addr)
21}