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:
David Pedersen
2021-07-31 21:36:30 +02:00
committed by GitHub
parent 407aa533d7
commit f67abd1ee2
6 changed files with 333 additions and 5 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ bytes = "1.0"
futures-util = "0.3"
http = "0.2"
http-body = "0.4"
hyper = { version = "0.14", features = ["server", "tcp"] }
hyper = { version = "0.14", features = ["server", "tcp", "http1"] }
pin-project = "1.0"
regex = "1.5"
serde = "1.0"