16 lines
449 B
Python
16 lines
449 B
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from fastapi import FastAPI, Request
|
|
|
|
|
|
def install_request_context(application: FastAPI) -> None:
|
|
@application.middleware("http")
|
|
async def request_context(request: Request, call_next):
|
|
request_id = uuid.uuid4().hex
|
|
request.state.request_id = request_id
|
|
response = await call_next(request)
|
|
response.headers["X-Request-ID"] = request_id
|
|
return response
|