Download app/api/middleware.py from sitcod3/modul-x: direct link, hf CLI and curl.
- Browser
- Download file 875 Bytes
-
https://huggingface.co/spaces/sitcod3/modul-x/resolve/main/app/api/middleware.py
- Command line
-
hf download hf://spaces/sitcod3/modul-x/app/api/middleware.py
-
curl -L -o middleware.py https://huggingface.co/spaces/sitcod3/modul-x/resolve/main/app/api/middleware.py
875 Bytes
| """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.""" | |
| 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 | |