Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extractor for remote connection info #55

Merged
merged 9 commits into from
Jul 31, 2021
Merged

Conversation

davidpdrsn
Copy link
Member

Fixes #43

With this you can get the remote address like so:

use axum::{prelude::*, extract::ConnectInfo};
use std::net::SocketAddr;

let app = route("/", get(handler));

async fn handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> String {
    format!("Hello {}", addr)
}

// Starting the app with `into_make_service_with_connect_info` is required
// for `ConnectInfo` to work.
let make_svc = app.into_make_service_with_connect_info::<SocketAddr, _>();

hyper::Server::bind(&"0.0.0.0:3000".parse().unwrap())
    .serve(make_svc)
    .await
    .expect("server failed");

This API is fully generic and supports whatever transport layer you're using with Hyper. I've updated the unix domain socket example to extract peer_creds and peer_addr.

@davidpdrsn davidpdrsn merged commit f67abd1 into main Jul 31, 2021
@davidpdrsn davidpdrsn deleted the connect-info-extractor branch July 31, 2021 19:36
@Roguelazer
Copy link

I've only had time to give this a cursory glance, but it looks great! Thanks for looking into this so fast!

@davidpdrsn davidpdrsn mentioned this pull request Aug 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extractor for request remote address
2 participants