-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Require less/no boiler plate to allow custom middleware to extract State
#1603
Comments
Yes I agree that would be nice but we don't currently know how to do it. The issue is that If we changed the signature of |
I could see tower adopting state as a concept, but I wonder whether passing it by shared reference and cloning where needed really makes sense for tower as a whole, and what the consequences would be if tower adopted a slightly different state concept. |
What we could do I guess is have our own layer trait that is automatically implemented for every tower layer. Right? |
Yeah that makes sense. So potentially, just thinking out loud, we could have that layer update the Request object when it is passed to the call But before I say anything further I need to go through the code. Right now I'm just learning the Axum api. But if this is possible without disturbing the existing Router::layer and ServiceBuilder::layer api, perhaps we could even extend them with wrappers?, it would be a fun challenge to try and implement it. |
Yeah we could try that and see how it works but it would also require our own
Its not possible without at least changing Not sure what you mean by "wrappers".
You're very welcome to give it a shot! However state propagation is probably the most complex part of axum so might be a little challenge depending on your level of experience. Digging into |
State
Fan and enthusiast here. I too am new For the sake of discussion, an abridged integration looks like: use di::*;
use di_axum::*;
#[injectable]
struct Person;
impl Person {
fn greet(&self) -> &str {
"Hello world!"
}
}
async fn say_hello(Inject(person): Inject<Person>) -> String {
person.greet().into()
}
#[tokio::main]
async fn main() {
let provider = ServiceCollection::new()
.add(Person::scoped())
.build_provider()
.unwrap();
let app = Router::new()
.route("/", get(say_hello))
.with_provider(provider);
let listener = TcpListener::bind("127.0.0.1:5000").await.unwrap();
axum::serve(listener, app).await.unwrap();
} This does not use As I'm still new to the ecosystem, I'm wondering if this concept can or should be pushed down further in to Should you be curious as to what a runnable example looks like, you can find one here. It is a reimagining of this |
Feature Request
It would be nice if it required much less boiler plate to get AppState passed as a parameter to a custom middleware function. The best would be if no additional code was needed at all and the AppState was simply included into the function's parameter signature, like how handlers are allowed to do.
Motivation
In larger projects there could be multiple layers of middleware and it can be tedious to impl all the Traits just to get access to AppState
Proposal
Ideally, simply allow all custom middleware to simply add AppState as one of their parameters during definition of the function
Alternatives
Not as far as I know.
Additional Comment
I am new to Axum but really like it. If someone more experienced could comment on feasibility of this feature, I'd be happy to take a stab at implementing it myself because I really want it.
Note I'm referring to this https://docs.rs/axum/latest/axum/middleware/index.html#accessing-state-in-middleware
The text was updated successfully, but these errors were encountered: