|
1 | | - |
2 | 1 | Creating Middleware |
3 | 2 | =================== |
4 | 3 |
|
@@ -47,6 +46,91 @@ outgoing responses: |
47 | 46 | :language: python |
48 | 47 |
|
49 | 48 |
|
| 49 | +Configuration constraints |
| 50 | +++++++++++++++++++++++++++ |
| 51 | + |
| 52 | +While it's good practice to keep middlewares decoupled from another, there are times |
| 53 | +where implicit coupling is unavoidable due to the nature of the functionality provided |
| 54 | +by the middlewares. |
| 55 | + |
| 56 | +For example a caching middleware and an authentication middleware |
| 57 | +can produce very different results depending on the order they are applied in; Assuming |
| 58 | +a naive caching middleware that does not take authentication state into account, if it's |
| 59 | +applied *before* the authentication middleware, it might cache an authenticated response |
| 60 | +and serve it to the next, unauthenticated request. |
| 61 | + |
| 62 | +Especially when applications grow larger and more complex, it can become difficult to |
| 63 | +keep track of all these implicit couplings and dependencies, or downright impossible if |
| 64 | +the middleware is implemented in a separate package and has no knowledge about how it is |
| 65 | +being applied. |
| 66 | + |
| 67 | +To help with this, :class:`~litestar.middleware.ASGIMiddleware` allows to specify a set |
| 68 | +of :class:`~litestar.middleware.constraints.MiddlewareConstraints` - Once configured, |
| 69 | +these will be validated on application startup. |
| 70 | + |
| 71 | +Using constraints, the example given above might be solved like this: |
| 72 | + |
| 73 | +.. literalinclude:: /examples/middleware/constraints.py |
| 74 | + :language: python |
| 75 | + |
| 76 | +Here, we specify that every instance of ``CachingMiddleware`` must come after any |
| 77 | +instance of |
| 78 | +:class:`~litestar.middleware.authentication.AbstractAuthenticationMiddleware`. |
| 79 | + |
| 80 | + |
| 81 | +.. tip:: |
| 82 | + |
| 83 | + When referencing classes, the constraints always apply to all instances and |
| 84 | + subclasses of the type |
| 85 | + |
| 86 | + |
| 87 | +Forward references |
| 88 | +~~~~~~~~~~~~~~~~~~ |
| 89 | + |
| 90 | +Constraints that reference other middleware can use strings as forward references, to |
| 91 | +handle situations like circular imports or middlewares from packages that may not be |
| 92 | +available: |
| 93 | + |
| 94 | +.. literalinclude:: /examples/middleware/constraints_string_ref.py |
| 95 | + :language: python |
| 96 | + |
| 97 | +This forward reference will try to import ``SomeMiddleware`` from |
| 98 | +``some_package.some_module``. With ``ignore_import_error=True``, if the import is not |
| 99 | +successful, the constraint will be ignored. |
| 100 | + |
| 101 | + |
| 102 | +Middleware order |
| 103 | +~~~~~~~~~~~~~~~~ |
| 104 | + |
| 105 | +For order constraints (``before``, ``after``, ``first``, ``last``), it is important to |
| 106 | +note that the order is defined in terms of proximity to the location. In practice, this |
| 107 | +means that a middleware that has set ``first=True`` must be the *first* middleware on |
| 108 | +the *first* layer (i.e. the application), and a middleware setting ``last=True`` must |
| 109 | +be the *last* middleware on the *last* layer (i.e. the route handler). |
| 110 | + |
| 111 | +.. code-block:: python |
| 112 | +
|
| 113 | + @get("/", middleware=[FifthMiddleware, SixthMiddleware]) |
| 114 | + async def handler() -> None: |
| 115 | + pass |
| 116 | +
|
| 117 | + router = Router( |
| 118 | + "/", |
| 119 | + [handler], |
| 120 | + middleware=[ |
| 121 | + ThirdMiddleware(), |
| 122 | + FourthMiddleware() |
| 123 | + ] |
| 124 | + ) |
| 125 | +
|
| 126 | + app = Litestar( |
| 127 | + middleware=[ |
| 128 | + FirstMiddleware(), |
| 129 | + SecondMiddleware() |
| 130 | + ] |
| 131 | + ) |
| 132 | +
|
| 133 | +
|
50 | 134 |
|
51 | 135 | Migrating from ``MiddlewareProtocol`` / ``AbstractMiddleware`` |
52 | 136 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
0 commit comments