-
Notifications
You must be signed in to change notification settings - Fork 118
Litestar plugin #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Litestar plugin #447
Changes from 6 commits
a5b959f
8a6f40d
4481d34
0fe5013
58d9230
51db172
5dcb29f
6ac9df9
18528d2
15b7595
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| __all__ = [ | ||
| "DishkaMiddleware", | ||
| "DishkaPlugin", | ||
| "FromDishka", | ||
| "LitestarProvider", | ||
| "inject", | ||
|
|
@@ -12,7 +14,11 @@ | |
| from typing import ParamSpec, TypeVar, get_type_hints | ||
|
|
||
| from litestar import Controller, Litestar, Request, Router, WebSocket | ||
| from litestar.config.app import AppConfig | ||
| from litestar.enums import ScopeType | ||
| from litestar.middleware import ASGIMiddleware | ||
| from litestar.plugins import InitPlugin | ||
| from litestar.types import ASGIApp, Receive, Scope, Send | ||
| from litestar.handlers import ( | ||
| BaseRouteHandler, | ||
| HTTPRouteHandler, | ||
|
|
@@ -165,3 +171,46 @@ def setup_dishka(container: AsyncContainer, app: Litestar) -> None: | |
| app.asgi_handler, | ||
| ) | ||
| app.state.dishka_container = container | ||
|
|
||
|
|
||
| class DishkaMiddleware(ASGIMiddleware): | ||
| scopes = (ScopeType.HTTP, ScopeType.WEBSOCKET) | ||
| async def handle( | ||
| self, scope: Scope, receive: Receive, | ||
| send: Send, next_app: ASGIApp, | ||
| ) -> None: | ||
| if scope.get("type") not in (ScopeType.HTTP, ScopeType.WEBSOCKET): | ||
| await next_app(scope, receive, send) | ||
| return | ||
|
|
||
| if scope.get("type") == ScopeType.HTTP: | ||
| request: Request = Request(scope) | ||
| r_context = {Request: request} | ||
| di_scope = DIScope.REQUEST | ||
| async with request.app.state.dishka_container( | ||
| r_context, | ||
| scope=di_scope, | ||
| ) as request_container: | ||
| request.state.dishka_container = request_container | ||
| await next_app(scope, receive, send) | ||
|
|
||
| elif scope.get("type") == ScopeType.WEBSOCKET: | ||
| websocket: WebSocket = WebSocket(scope) | ||
| w_context = {WebSocket: websocket} | ||
| di_scope = DIScope.SESSION | ||
| async with websocket.app.state.dishka_container( | ||
| w_context, | ||
| scope=di_scope, | ||
| ) as request_container: | ||
| websocket.state.dishka_container = request_container | ||
| await next_app(scope, receive, send) | ||
|
|
||
|
|
||
| class DishkaPlugin(InitPlugin): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add plugin automatically inside
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think so, the current api is and this PR just adds the possibility to do: I dont know a litestar's built-in way to add a plugin other than using the plugins kwarg unfortunately. |
||
| def __init__(self, container: AsyncContainer): | ||
| self.container = container | ||
|
|
||
| def on_app_init(self, app_config: AppConfig) -> AppConfig: | ||
| app_config.state.dishka_container = self.container | ||
| app_config.middleware.append(DishkaMiddleware()) | ||
| return app_config | ||
Uh oh!
There was an error while loading. Please reload this page.