Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions crates/api/src/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// API key authentication middleware for admin routes.
// + Request ID middleware for trace propagation across services.
use axum::{
body::Body,
extract::State,
Expand All @@ -11,6 +12,31 @@ use serde_json::json;

use crate::state::AppState;


// Request ID middleware: reads X-Request-Id header or generates a UUIDv4,
// injects it into response headers, and attaches it to the tracing span.
pub async fn request_id(
req: Request<Body>,
next: Next,
) -> Response {
let request_id = req
.headers()
.get("x-request-id")
.and_then(|v| v.to_str().ok())
.map(str::to_string)
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());

tracing::Span::current().record("request_id", &request_id);

let mut response = next.run(req).await;
response.headers_mut().insert(
"x-request-id",
request_id.parse().unwrap(),
);
response
}


pub async fn require_admin_key(
State(state): State<AppState>,
req: Request<Body>,
Expand Down
26 changes: 26 additions & 0 deletions crates/gateway/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,37 @@ use crate::attestation::{
use crate::state::AppState;
use crate::webhook::{parse_merge_event, verify_github_signature};

// Request ID middleware for trace propagation across services.
async fn request_id(req: Request<Body>, next: Next) -> Response {
use axum::body::Body;
use axum::http::Request;
use axum::middleware::Next;
use axum::response::Response;

let request_id = req
.headers()
.get("x-request-id")
.and_then(|v| v.to_str().ok())
.map(str::to_string)
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());

tracing::Span::current().record("request_id", &request_id);

let mut response = next.run(req).await;
response.headers_mut().insert(
"x-request-id",
request_id.parse().unwrap(),
);
response
}


pub fn router(state: AppState) -> Router {
Router::new()
.route("/health", get(health))
.route("/ready", get(ready))
.route("/webhooks/github", post(github_webhook))
.layer(axum::middleware::from_fn(request_id))
.with_state(state)
}

Expand Down