-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Allow using Option<Middleware>
to enable/disable a middlware
#2623
base: master
Are you sure you want to change the base?
Conversation
I like this and I think it is cleaner for And thanks again for your contribution :) |
Why would that be a breaking change? It only adds a new struct. |
I meant changing I prefer having the implementation on |
69498c0
to
61f7cbb
Compare
Thanks @aliemjay … that makes sense. I rebased the PR and made the suggested changes. |
No Condition not always produce middleware service. See:
Condition only call new_transform and produce middleware service when you pass true to it. You always provide the factory type for the middleware but it's not used in anyway when disabled by condition. I don't see the point of this PR. It's like a misunderstanding of the service initialization to me. |
It is pretty useful for middlewares that doesn't provide an easy default. Consider something like |
|
Not at all. That is the point of this PR to enable this. |
This comment was marked as off-topic.
This comment was marked as off-topic.
61f7cbb
to
5466d88
Compare
I moved this into an "extras" crate, so that it can be used in the meantime: https://crates.io/crates/actix-web-extras |
Use external crate for the `Optional` middleware, which was a fork of the `Condition` middleware. Hopefully PR actix/actix-web#2623 gets merged so that we can drop this. For now, we have a stable solution.
Use external crate for the `Optional` middleware, which was a fork of the `Condition` middleware. Hopefully PR actix/actix-web#2623 gets merged so that we can drop this. For now, we have a stable solution.
eb7faf8
to
38f618f
Compare
I rebased the PR and added an entry in the changelog. The documentation was already there. |
Currently, there is `Condition`, which accepts a boolean (to enable/disable) and an instance to the actual middleware. The downside of that is, that such a middleware needs to be constructed in any case. Even if the middleware is used or not. However, the middleware is not used when it is disabled. Only the type seems required. So this PR adds a `from_option` function, which allows passing in an `Option` instead of boolean and instance. If the option "is some" it is enabled. Otherwise, not.
38f618f
to
1c703ac
Compare
PR Type
Feature
PR Checklist
Overview
Currently, there is
Condition
, which accepts a boolean (to enable/disable) and an instance tothe actual middleware. The downside of that is, that such a middlware needs to be constructed in
any case. Even if the middleware is used or not.
However, the middleware is not used when it is disabled. Only the type seems required. So this
Optional
middleware allows passing in anOption
instead of boolean and instance. If the option"is some" it is enabled. Otherwise not.
Improves #934