Skip to content
Myr

main.rs

This file

Path

examples/request-id/src/main.rs

64 lines · 2.1 KiB · Rust

Last changed by

examples: Refactor request-id example (#3865)tottotobefore adoption97def95

History of this file

Take it

The walk stopped after 200 commits: lines older than dcbcf5c point at it.

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

lines 1–6why, one step back
1//! Run with
2//!3//! ```not_rust4//! cargo run -p example-request-id5//! ```6

examples: Refactor request-id example (#3865)tottotobefore adoption97def95

line 7why, one step back
7use axum::{http::Request, response::Html, routing::get, Router};

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

lines 8–9why, one step back
8use tower::ServiceBuilder;
9use tower_http::{

examples: Refactor request-id example (#3865)tottotobefore adoption97def95

line 10why, one step back
10 request_id::{MakeRequestUuid, RequestId},

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

line 11why, one step back
11 trace::TraceLayer,

examples: Refactor request-id example (#3865)tottotobefore adoption97def95

line 12why, one step back
12 ServiceBuilderExt,

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

line 13why, one step back
13};

examples: Refactor request-id example (#3865)tottotobefore adoption97def95

line 14why, one step back
14use tracing::{info, info_span};

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

lines 15–34why, one step back
15use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
1617#[tokio::main]18async fn main() {19 tracing_subscriber::registry()20 .with(21 tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {22 // axum logs rejections from built-in extractors with the `axum::rejection`23 // target, at `TRACE` level. `axum::rejection=trace` enables showing those events24 format!(25 "{}=debug,tower_http=debug,axum::rejection=trace",26 env!("CARGO_CRATE_NAME")27 )28 .into()29 }),30 )31 .with(tracing_subscriber::fmt::layer())32 .init();3334 let middleware = ServiceBuilder::new()

examples: Refactor request-id example (#3865)tottotobefore adoption97def95

line 35why, one step back
35 .set_x_request_id(MakeRequestUuid)

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

lines 36–38why, one step back
36 .layer(
37 TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {38 // Log the request id as generated.

examples: Refactor request-id example (#3865)tottotobefore adoption97def95

line 39why, one step back
39 let request_id = request.extensions().get::<RequestId>().unwrap();

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

line 40why, one step back
40

examples: Refactor request-id example (#3865)tottotobefore adoption97def95

lines 41–43why, one step back
41 match request_id.header_value().to_str() {
42 Ok(request_id) => info_span!("http_request", request_id),43 Err(_) => info_span!("http_request", request_id = ?request_id),

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

lines 44–47why, one step back
44 }
45 }),46 )47 // send headers from request to response headers

examples: Refactor request-id example (#3865)tottotobefore adoption97def95

line 48why, one step back
48 .propagate_x_request_id();

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

lines 49–57why, one step back
49
50 // build our application with a route51 let app = Router::new().route("/", get(handler)).layer(middleware);5253 // run it54 let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")55 .await56 .unwrap();57 println!("listening on {}", listener.local_addr().unwrap());

Fix return types of serve futures (#3601)Jonas Plattebefore adoptionf3a95d7

line 58why, one step back
58 axum::serve(listener, app).await;

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

lines 59–64why, one step back
59}
6061async fn handler() -> Html<&'static str> {62 info!("Hello world!");63 Html("<h1>Hello, World!</h1>")64}