File size: 875 Bytes
5db9737 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | """API middleware: request logging and timing helpers.
Rate limiting and CORS are wired directly in main.py; this module
provides a reusable timing/logging middleware.
"""
import time
import structlog
from fastapi import Request
logger = structlog.get_logger()
class RequestLogger:
"""ASGI-style timing/logging helper used from main.py middleware."""
@staticmethod
async def __call__(request: Request, call_next):
start = time.monotonic()
response = await call_next(request)
elapsed = (time.monotonic() - start) * 1000
response.headers["X-Process-Time"] = f"{elapsed:.2f}ms"
logger.info(
"request",
method=request.method,
path=request.url.path,
status=response.status_code,
ms=round(elapsed, 2)
)
return response
|