Add middleware.py
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
import logging
|
||||||
|
from typing import Awaitable, Callable
|
||||||
|
|
||||||
|
class SimpleMiddleware:
|
||||||
|
"""A minimal ASGI middleware that logs incoming request paths.
|
||||||
|
|
||||||
|
This middleware is deliberately lightweight and does not depend on
|
||||||
|
any external libraries. It can be wrapped around any ASGI application
|
||||||
|
(e.g., FastAPI, Starlette) to provide basic request logging.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, app: Callable):
|
||||||
|
self.app = app
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
async def __call__(self, scope, receive, send):
|
||||||
|
if scope.get("type") == "http":
|
||||||
|
path = scope.get("path", "<unknown>")
|
||||||
|
self.logger.info(f"Incoming request: {path}")
|
||||||
|
await self.app(scope, receive, send)
|
||||||
|
|
||||||
|
# Helper function for convenience
|
||||||
|
|
||||||
|
def get_middleware(app: Callable) -> SimpleMiddleware:
|
||||||
|
"""Return an instance of :class:`SimpleMiddleware` wrapped around *app*.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
app: Callable
|
||||||
|
The ASGI application to wrap.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
SimpleMiddleware
|
||||||
|
The middleware instance.
|
||||||
|
"""
|
||||||
|
return SimpleMiddleware(app)
|
||||||
Reference in New Issue
Block a user