-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathconfigurable.rs
More file actions
41 lines (34 loc) · 1.07 KB
/
configurable.rs
File metadata and controls
41 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! An example of configuring `ClientIp` using an environment variable
//!
//! Don't forget to set the variable before running, e.g.:
//! ```sh
//! IP_SOURCE=ConnectInfo cargo run --example configurable
//! ```
use std::net::SocketAddr;
use axum::{Router, routing::get};
use axum_client_ip::{ClientIp, ClientIpSource};
#[derive(serde::Deserialize)]
struct Config {
ip_source: ClientIpSource,
}
async fn handler(ClientIp(ip): ClientIp) -> 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());
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
println!("Listening on http://localhost:3000/");
axum::serve(
listener,
// Required for `ClientIpSource::ConnectInfo`
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.await
.unwrap()
}