# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-axum = "0.5"
+axum = { version = "0.5", features = ["headers"] }
tokio = { version = "1", features = ["full"] }
tower = "0.4"
tower-http = { version = "0.3", features = ["trace", "fs"] }
use axum::{
routing::{get, get_service},
- extract::Path,
+ extract::{Path, TypedHeader},
http::{StatusCode},
response,
Router};
-async fn hello(Path(name): Path<String>) -> impl response::IntoResponse {
- (StatusCode::OK, format!("Hello {}", name))
+async fn hello(Path(name): Path<String>, user_agent: Option<TypedHeader<axum::headers::UserAgent>>) -> impl response::IntoResponse {
+ if let Some(TypedHeader(user_agent)) = user_agent {
+ (StatusCode::OK, format!("Hello {} from {}", name, user_agent))
+ } else {
+ (StatusCode::OK, format!("Hello {}", name))
+ }
}
#[tokio::main]
)
.layer(
tower::ServiceBuilder::new()
- .layer(tower_http::trace::TraceLayer::new_for_http())
+ .layer(
+ tower_http::trace::TraceLayer::new_for_http()
+ .make_span_with(tower_http::trace::DefaultMakeSpan::default().include_headers(true)),
+ )
);
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));