Request Chain ID

The Request ID middleware is highly flexible. The generator (IdGenerator) is used to create IDs, and you can define your own ID generator as long as it implements the IdGenerator trait. The default generator provided is UlidGenerator.

Additionally, you can control whether to overwrite an existing requestid. You can also configure settings such as header_name. For details, please refer to the documentation.

Example Code

main.rs
Cargo.toml
request-id/src/main.rs
use salvo::prelude::*;

#[handler]
async fn hello(req: &mut Request) -> String {
format!("Request id: {:?}", req.header::<String>("x-request-id"))
}

#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();

let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
let router = Router::new().hoop(RequestId::new()).get(hello);
Server::new(acceptor).serve(router).await;
}