You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ellar applies some ASGI middleware necessary for resource protection, error handling, and context management.
65
45
They include:
@@ -69,9 +49,11 @@ They include:
69
49
-**`RequestServiceProviderMiddleware`**: - This inherits from `ServerErrorMiddleware`. It provides DI context during request and
70
50
also ensures that application exceptions may return a custom 500 page, or display an application traceback in DEBUG mode.
71
51
-**`RequestVersioningMiddleware`**: This computes resource versioning info from request object based on configured resource versioning scheme at the application level.
72
-
-**`ExceptionMiddleware`**: - Adds exception handlers, so that particular types of expected exception cases can be associated with handler functions. For example raising `HTTPException(status_code=404)` within an endpoint will end up rendering a custom 404 page.
52
+
-**`ExceptionMiddleware`**: - Adds exception handlers, so that some common exception raised with the application can be associated with handler functions. For example raising `HTTPException(status_code=404)` within an endpoint will end up rendering a custom 404 page.
53
+
-**`SessionMiddleware`**: controls session state using the session strategy configured in the application.
54
+
-**`IdentityMiddleware`**: controls all registered authentication schemes and provides user identity to all request
73
55
74
-
## Applying Middleware
56
+
## **Applying Middleware**
75
57
Middleware can be applied through the application `config` - `MIDDLEWARES` variable.
76
58
77
59
Let's apply some middleware in our previous project. At the project root level, open `config.py`.
@@ -95,6 +77,36 @@ class DevelopmentConfig(BaseConfig):
95
77
!!! Hint
96
78
This is how to apply any `ASGI` middlewares such as `GZipMiddleware`, `EllarASGIMiddlewareStructure`, and others available in the `Starlette` library.
97
79
80
+
## **Dependency Injection**
81
+
In section above, we saw how middleware are registered to the application. But what if the middleware class depends on other services, how then should we configure it?
82
+
The `Middleware` does all the work.
83
+
84
+
For example, lets modify the `GZipMiddleware` class and make it depend on `Config` service.
85
+
```python
86
+
from ellar.core import Config
87
+
from ellar.core.middleware import GZipMiddleware, Middleware
These middlewares can be configured as every other asgi middleware as shown in middleware [docs](../../overview/middleware/#applying-middleware) to work in Ellar
11
+
12
+
For example, using [Starlette CSRF](https://pypi.org/project/starlette-csrf/) Middleware
0 commit comments