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: