aboutsummaryrefslogtreecommitdiffstats
path: root/src/server/middleware/request_id.rs
blob: 7163c860f9c76f7e3b0fa2803a1a474b01b39bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use axum::{
    extract::Request,
    http::{HeaderValue, StatusCode},
    middleware::Next,
    response::Response,
};
use uuid::Uuid;

pub const REQUEST_ID_HEADER: &str = "x-request-id";

pub async fn add_request_id(mut request: Request, next: Next) -> Result<Response, StatusCode> {
    let headers = request.headers_mut();
    let id = Uuid::now_v7().to_string();
    tracing::trace!(id = id, "attaching request id");
    let bytes = id.as_bytes();

    headers.insert(REQUEST_ID_HEADER, HeaderValue::from_bytes(bytes).unwrap());

    Ok(next.run(request).await)
}