From 16d9ab7e75a37a4ba8bec1f03a51c0d26943663f Mon Sep 17 00:00:00 2001 From: balabanovan530 <175+balabanovan530@noreply.localhost> Date: Thu, 28 May 2026 13:25:26 +0000 Subject: [PATCH] Add middleware.py --- middleware.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 middleware.py diff --git a/middleware.py b/middleware.py new file mode 100644 index 0000000..7c968fe --- /dev/null +++ b/middleware.py @@ -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", "") + 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)