use axum::extract::Request;
use axum::middleware::Next;
use axum::response::Response;
use uuid::Uuid;
pub async fn add_request_id(mut request: Request, next: Next) -> Response {
let request_id = Uuid::new_v4().to_string();
request
.extensions_mut()
.insert(RequestId(request_id.clone()));
let span = tracing::Span::current();
span.record("request_id", &request_id);
let mut response = next.run(request).await;
response
.headers_mut()
.insert("x-request-id", request_id.parse().unwrap());
response
}
#[allow(
dead_code,
reason = "pub(crate) web infrastructure used by route configuration"
)]
#[derive(Debug, Clone)]
pub struct RequestId(pub String);
impl RequestId {
#[allow(
dead_code,
reason = "pub(crate) web infrastructure used by route configuration"
)]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_request_id_creation() {
let id = RequestId("test-request-123".to_string());
assert_eq!(id.as_str(), "test-request-123");
}
#[test]
fn test_request_id_as_str() {
let uuid_str = "550e8400-e29b-41d4-a716-446655440000";
let id = RequestId(uuid_str.to_string());
assert_eq!(id.as_str(), uuid_str);
}
#[test]
fn test_request_id_clone() {
let original = RequestId("original-id".to_string());
let cloned = original.clone();
assert_eq!(original.as_str(), cloned.as_str());
}
#[test]
fn test_request_id_debug() {
let id = RequestId("debug-test".to_string());
let debug_str = format!("{:?}", id);
assert!(debug_str.contains("debug-test"));
}
#[test]
fn test_request_id_empty_string() {
let id = RequestId(String::new());
assert_eq!(id.as_str(), "");
}
}