/// Middleware to include request ID in response header. pub struct RequestId; impl actix_web::dev::Transform for RequestId where S: actix_web::dev::Service< actix_web::dev::ServiceRequest, Response = actix_web::dev::ServiceResponse, Error = actix_web::Error, >, S::Future: 'static, B: 'static, { type Response = actix_web::dev::ServiceResponse; type Error = actix_web::Error; type Transform = RequestIdMiddleware; type InitError = (); type Future = std::future::Ready>; fn new_transform(&self, service: S) -> Self::Future { std::future::ready(Ok(RequestIdMiddleware { service })) } } pub struct RequestIdMiddleware { service: S, } impl actix_web::dev::Service for RequestIdMiddleware where S: actix_web::dev::Service< actix_web::dev::ServiceRequest, Response = actix_web::dev::ServiceResponse, Error = actix_web::Error, >, S::Future: 'static, B: 'static, { type Response = actix_web::dev::ServiceResponse; type Error = actix_web::Error; type Future = futures::future::LocalBoxFuture<'static, Result>; actix_web::dev::forward_ready!(service); fn call(&self, req: actix_web::dev::ServiceRequest) -> Self::Future { let mut req = req; let request_id_fut = req.extract::(); let response_fut = self.service.call(req); Box::pin(async move { let request_id = request_id_fut.await?; let mut response = response_fut.await?; response.headers_mut().append( http::header::HeaderName::from_static("x-request-id"), http::HeaderValue::from_str(&request_id.as_hyphenated().to_string())?, ); Ok(response) }) } } /// Middleware for attaching default response headers. Headers with the same key already set in a /// response will not be overwritten. pub fn default_response_headers() -> actix_web::middleware::DefaultHeaders { use actix_web::http::header; actix_web::middleware::DefaultHeaders::new() // Max age of 1 year in seconds, equal to `60 * 60 * 24 * 365` seconds. .add((header::STRICT_TRANSPORT_SECURITY, "max-age=31536000")) .add((header::VIA, "HyperSwitch")) }