-
-
Notifications
You must be signed in to change notification settings - Fork 375
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
Enhancement: fully support builder pattern for apps #3257
Comments
What problem would this solve that an At the moment I'm not convinced that this is a good feature to add. We are actively trying to get away from the mutable app instances, and making this work while taking care of all the edge cases is going to be quite a significant amount of work which I think will cause more trouble down the road than it's worth. I also think it's important that we keep things as simple as possible and don't offer too many ways to do similar things. Right now, we already have a pattern in place that allows to fully customise application creation and configuration, and that is plugins. @bunny-therapist If you have a real world example of where plugins cannot solve the issue in a decent way, I think we should first try to see how we can make this solvable with plugins. @litestar-org/maintainers |
Can an InitPlugin add middleware that affects all the route handlers of the app, including the ones that were passed as |
Yes. |
Thanks. I will try that on Tuesday and then get back to you. If the intention is for |
Fully agree with you there, and in 3.0 things are probably actually going to be a lot more immutable :) |
Ok, an InitPlugin worked like a charm. With the caveat that modifying |
@bunny-therapist I'm going to close this one then, feel free to open up a new one for the issues you're having with the |
@bunny-therapist Do you have this public by chance? :D |
Summary
When you create an app (instantiate
litestar.Litestar
) you can pass in everything - route handlers, middleware, response headers, etc to the__init__
- and you get a fully functional app. Some of the properties of the app can be modified after instantiation, e.g.,litestar.Litestar.register
is a method that exists solely for adding new route handlers.litestar.Litestar.response_headers
is just a public-attribute list and can thus be modified (not sure if this is intentional, the typing makes things a bit awkward), and the modifications affect the app.litestar.Litestar.cache_control
is - I think - similar to the response headers.However, modifying
litestar.Litestar.middleware
does not affect the routes of the app (since they have already been "resolved"), so even though this is a public-attribute list, modifying it does not work as one might expect.The existence of
litestar.Litestar.register
seems to imply the intention of supporting a builder pattern, where the app can be constructed one piece at a time, adding route handlers, middleware, etc. However, given that (at least) middleware does not affect already resolved routes, the builder pattern is not really supported. Even if you add more middleware to the app instance, they will not take effect.I do not know what the best interface would be, but I would like litestar to support modification of all attributes of the app post-initialization so that apps can be built piece by piece.
See discussion at https://github.com/orgs/litestar-org/discussions/3244
Basic Example
Drawbacks and Impact
The suggested changes should not affect any backwards compatibility or have any significant drawbacks, unless someone, somewhere is, e.g., creating app with route handlers, then adding middleware, then registering a new endpoint, and expecting only the new endpoint to use the middleware - and this behavior changes.
If the feature is implemented as suggested in the discussion - with a
litestar.Litestar.recreate
method, then there should be no concerns at all, since it is just adding a new method and everything else works the same.Unresolved questions
What is the intended way users would use the builder pattern?
Are attributes like
litestar.Litestar.response_headers
public and mutable intentionally, or just a consequence of inheritance? At a minimum, the behavior of modifying apps post-initialization must be properly documented.Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: