Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ctron
Copy link

@ctron ctron commented Feb 2, 2022

PR Type

Feature

PR Checklist

  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • A changelog entry has been made for the appropriate packages.
  • Format code with the latest stable rustfmt.
  • (Team) Label with affected crates and semver status.

Overview

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 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 an Option instead of boolean and instance. If the option
"is some" it is enabled. Otherwise not.

Improves #934

@aliemjay
Copy link
Member

aliemjay commented Feb 2, 2022

I like this and I think it is cleaner for Condition::new to already accept Option. But since this would arguably be an unneccessary breaking change, I think it is better to be implemented as Condition::from_option or impl From<Option<T>> for Condition<T>.

And thanks again for your contribution :)

@robjtede robjtede added this to the actix-web post-v4 milestone Feb 2, 2022
@ctron
Copy link
Author

ctron commented Feb 2, 2022

But since this would arguably be an unneccessary breaking change, …

Why would that be a breaking change? It only adds a new struct.

@aliemjay
Copy link
Member

aliemjay commented Feb 2, 2022

Why would that be a breaking change?

I meant changing Condition::new to accept Option would be breaking. Sorry for the confusion.

I prefer having the implementation on Condition because it is less api suface area and thus less maintenance burden than adding a new struct. You can change Condition internals to store an option and that would make using it either way possible Condition::{new, from_option}

@ctron ctron force-pushed the feature/optional_mw_1 branch from 69498c0 to 61f7cbb Compare February 3, 2022 16:14
@ctron
Copy link
Author

ctron commented Feb 3, 2022

Thanks @aliemjay … that makes sense.

I rebased the PR and made the suggested changes.

@fakeshadow
Copy link
Contributor

No Condition not always produce middleware service. See:

let fut = self.transformer.new_transform(service);

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.

@aliemjay
Copy link
Member

aliemjay commented Feb 4, 2022

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 Condition::from(std::env::var("ARG").map(|arg| MyMiddleware::config(arg)))

@fakeshadow
Copy link
Contributor

MyMiddleware is just a constructor for the real middleware.
Why you make it not have an easy constructor based on condition? If not how were you using Condition?

@ctron
Copy link
Author

ctron commented Feb 7, 2022

If not how were you using Condition?

Not at all. That is the point of this PR to enable this.

@ppamorim

This comment was marked as off-topic.

@ctron
Copy link
Author

ctron commented Mar 1, 2022

I moved this into an "extras" crate, so that it can be used in the meantime: https://crates.io/crates/actix-web-extras

ctron added a commit to jcrossley3/drogue-cloud that referenced this pull request Mar 1, 2022
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.
ctron added a commit to drogue-iot/drogue-cloud that referenced this pull request Mar 1, 2022
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.
@robjtede robjtede removed this from the actix-web post-v4 milestone Mar 1, 2022
@ctron ctron force-pushed the feature/optional_mw_1 branch 2 times, most recently from eb7faf8 to 38f618f Compare June 29, 2022 14:44
@ctron
Copy link
Author

ctron commented Jun 29, 2022

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.
@ctron ctron force-pushed the feature/optional_mw_1 branch from 38f618f to 1c703ac Compare June 29, 2022 14:50
@robjtede robjtede added B-semver-minor A-web project: actix-web labels Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-web project: actix-web B-semver-minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants