-
-
Notifications
You must be signed in to change notification settings - Fork 442
refactor: asgimiddleware everywhere if possible #4055
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: v3.0
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I went through everything and it's looking quite good so far!
# middleware=[SessionMiddleware(ServerSideSessionBackend(session_backend_config_memory)), | ||
# SessionAuthMiddleware(session_auth)], | ||
plugins=[SessionPlugin(session_auth)], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kept this now I remember to "showcase" the plugin is the same essentially as stacking the middlewares, now should you change their order and you're f**,
for middleware in handler_middleware: | ||
if not isinstance(middleware, CORSMiddleware) and not isinstance( | ||
middleware, OpenTelemetryInstrumentationMiddleware | ||
): | ||
asgi_handler = middleware(asgi_handler) | ||
|
||
# we keep the 2.x behaviour but add a check to see if the middleware is already in the stack, and raise a deprecation warning | ||
if app.csrf_config and not any(isinstance(middleware, CSRFMiddleware) for middleware in handler_middleware): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we move the deprecation warnings to Litestar.__init__
? Feels a bit odd in here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although on second thought, maybe it's okay to have them here, where they're actually being used
@@ -182,38 +183,61 @@ def build_route_middleware_stack( | |||
Returns: | |||
An ASGIApp that is composed of a "stack" of middlewares. | |||
""" | |||
|
|||
from litestar.contrib.opentelemetry import OpenTelemetryInstrumentationMiddleware |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to base my hotreload plugin of this branch and I get:
try:
import opentelemetry # noqa: F401
except ImportError as e:
> raise MissingDependencyException("opentelemetry") from e
E litestar.exceptions.base_exceptions.MissingDependencyException: Package 'opentelemetry' is not installed but required. You can install it by running 'pip install litestar[opentelemetry]' to install litestar with the required extra or 'pip install opentelemetry' to install the package separately
so I start to think the private _patch_opentelemetry_middleware is designed to handle the case where opentelemetry isnt installed ?
…iddleware I removed does ?
Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/4055 |
67069a4
to
02fdd06
Compare
WIP
The middlewares are currently mostly based on either
MiddlewareProtocol
,AbstractMiddleware
orAbstractAuthenticationMiddleware
.Their usage is spread around a few places:
Litestar(allowed_hosts=allowed_hosts_config)
app_init
kwargs on the Litestar object like inLitestar(on_app_init=[jwt_auth.on_app_init])
plugins
kwarg on the Litestar object.middleware
kwarg on the Litestar object trough themiddleware
property of the config, like for instanceLitestar(middleware=[LoggingMiddlewareConfig().middleware]))
middleware
like in@get("/", middleware=[jwt_auth.middleware])
The idea of this PR is to:
ASGIMiddleware
(andBaseAuthenticationMiddleware
which subclasses it)I think a logical subsequent step would be to remove entirely point 1 and 2 from the Litestar object, but that may go too far idk.
Current state:
csrf_config
cors_config
allowed_hosts
compression_config
response_cache_config
and 4Also, xxxMiddleware is often tied to xxxConfig, but there are some inconsistencies I didn't touch like for instance:
LoggingMiddlewareConfig
is in litestar/middleware/logging.py along with theLoggingMiddleware
BUT for others there is a litestar/config folder that contains the
CSRFConfig
for instance ie config and middleware are seperated,I can totally understand the split but since now xxxConfig is just a config for xxxMiddleware then I'd feel it would be better all are on the same middleware folder in their respective middleware