axum-client-ip 0.4.0

Client IP address extractors for Axum
Documentation
//! An example of configuring `SecureClientIp` using an env variable
//! Don't forget set the variable before running e.g.: `IP_SOURCE=ConnectInfo cargo run --example secure`
use axum::{routing::get, Router};
use axum_client_ip::{SecureClientIp, SecureClientIpSource};
use std::net::SocketAddr;

#[derive(serde::Deserialize)]
struct Config {
    ip_source: SecureClientIpSource,
}

async fn handler(SecureClientIp(ip): SecureClientIp) -> String {
    ip.to_string()
}

#[tokio::main]
async fn main() {
    let config: Config = envy::from_env().unwrap();

    let app = Router::new()
        .route("/", get(handler))
        // the line you're probably looking for :)
        .layer(config.ip_source.into_extension());

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service_with_connect_info::<SocketAddr>())
        .await
        .unwrap()
}