use crate::context::RequestContext;
use axum::extract::Request;
use axum::middleware::Next;
use axum::response::Response;

pub async fn inject_request_context(mut request: Request, next: Next) -> Response {
    let context =
        RequestContext::from_http_parts(request.method(), request.uri(), request.headers());
    request.extensions_mut().insert(context.clone());

    let mut response = next.run(request).await;
    context.apply_response_headers(response.headers_mut());
    response
}
