mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-25 00:00:23 +02:00
Add extractor for remote connection info (#55)
Fixes https://github.com/tokio-rs/axum/issues/43 With this you can get the remote address like so: ```rust 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`.
This commit is contained in:
@@ -260,12 +260,16 @@ use std::{
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
pub mod connect_info;
|
||||
pub mod extractor_middleware;
|
||||
pub mod rejection;
|
||||
|
||||
#[doc(inline)]
|
||||
pub use self::extractor_middleware::extractor_middleware;
|
||||
|
||||
#[doc(inline)]
|
||||
pub use self::connect_info::ConnectInfo;
|
||||
|
||||
#[cfg(feature = "multipart")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "multipart")))]
|
||||
pub mod multipart;
|
||||
@@ -904,6 +908,8 @@ where
|
||||
/// # hyper::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
|
||||
/// # };
|
||||
/// ```
|
||||
///
|
||||
/// [`Stream`]: https://docs.rs/futures/latest/futures/stream/trait.Stream.html
|
||||
#[derive(Debug)]
|
||||
pub struct BodyStream<B = crate::body::Body>(B);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user