From 0cc8e76aec87825ff62d8ead1937773bedbc48d6 Mon Sep 17 00:00:00 2001 From: Joe Kaufeld Date: Tue, 27 Aug 2024 09:09:05 -0400 Subject: [PATCH] :memo: layout and start of middleware --- docs/_sidebar.md | 8 +++++--- docs/middleware/csrf.md | 0 docs/middleware/overview.md | 19 +++++++++++++++++++ docs/middleware/pydantic.md | 0 docs/middleware/sessions.md | 0 docs/middleware/test.md | 1 - docs/middleware/test2.md | 1 - spiderweb/main.py | 1 + spiderweb/middleware/__init__.py | 2 +- 9 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 docs/middleware/csrf.md create mode 100644 docs/middleware/overview.md create mode 100644 docs/middleware/pydantic.md create mode 100644 docs/middleware/sessions.md delete mode 100644 docs/middleware/test.md delete mode 100644 docs/middleware/test2.md diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 1a1fd70..240e30b 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -1,6 +1,8 @@ - [home](README.md) - [quickstart](quickstart.md) - [responses](responses.md) -- Middleware - - [middleware](middleware/test.md) - - [middleware2](middleware/test2.md) \ No newline at end of file +- middleware + - [overview](middleware/overview.md) + - [csrf](middleware/csrf.md) + - [pydantic](middleware/pydantic.md) + - [session](middleware/sessions.md) diff --git a/docs/middleware/csrf.md b/docs/middleware/csrf.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/middleware/overview.md b/docs/middleware/overview.md new file mode 100644 index 0000000..475c9a3 --- /dev/null +++ b/docs/middleware/overview.md @@ -0,0 +1,19 @@ +# middleware + +When processing a request, there are often things that need to happen before the request is passed to the view. Middleware is a way to intercept the request and response objects and do something with them before they are passed to the view or returned to the client. + +Middleware in Spiderweb is defined as a list of importable strings that looks like this: + +```python +from spiderweb import SpiderwebRouter + +app = SpiderwebRouter( + middleware=[ + "spiderweb.middleware.sessions.SessionMiddleware", + "spiderweb.middleware.csrf.CSRFMiddleware", + "spiderweb.middleware.pydantic.PydanticMiddleware", + ], +) +``` + +Middleware affects both the incoming request AND the outgoing response, so the order that it's defined here is important. When a request comes in, it will be processed through the middleware in the order that it's defined, but after the response is created, it will pass back through the middleware going the opposite direction. \ No newline at end of file diff --git a/docs/middleware/pydantic.md b/docs/middleware/pydantic.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/middleware/sessions.md b/docs/middleware/sessions.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/middleware/test.md b/docs/middleware/test.md deleted file mode 100644 index 5e40c08..0000000 --- a/docs/middleware/test.md +++ /dev/null @@ -1 +0,0 @@ -asdf \ No newline at end of file diff --git a/docs/middleware/test2.md b/docs/middleware/test2.md deleted file mode 100644 index eb0fb96..0000000 --- a/docs/middleware/test2.md +++ /dev/null @@ -1 +0,0 @@ -asdfawaasdf \ No newline at end of file diff --git a/spiderweb/main.py b/spiderweb/main.py index ab149e0..8727932 100644 --- a/spiderweb/main.py +++ b/spiderweb/main.py @@ -39,6 +39,7 @@ logging.basicConfig(level=logging.INFO) class SpiderwebRouter(LocalServerMixin, MiddlewareMixin, RoutesMixin, FernetMixin): def __init__( self, + *, addr: str = None, port: int = None, db: Optional[Database] = None, diff --git a/spiderweb/middleware/__init__.py b/spiderweb/middleware/__init__.py index 32c9419..c43b43b 100644 --- a/spiderweb/middleware/__init__.py +++ b/spiderweb/middleware/__init__.py @@ -39,7 +39,7 @@ class MiddlewareMixin: def process_response_middleware( self, request: Request, response: HttpResponse ) -> None: - for middleware in self.middleware: + for middleware in reversed(self.middleware): try: middleware.process_response(request, response) except UnusedMiddleware: