Replies: 3 comments
-
So basically, you want to have a middleware that on some routes wraps a middleware that's applied to all routes in one place? Yeah, that's unfortunately not possible. Your "best bet" solution seems reasonable to me, though I personally find tuples-as-layers ( |
Beta Was this translation helpful? Give feedback.
-
You can try using |
Beta Was this translation helpful? Give feedback.
-
I'm already going to use MatchedPath in the Route-level middleware. I've asked elsewhere, but am finding that while that's useful, I can't figure out how to get the corresponding Path parameters for a Route-scoped middleware. That said, what's missing is the ability to e.g. make a database request to find the owner of a Comment and include that for authorization. It's debatable whether that's appropriate in a middleware anyway. |
Beta Was this translation helpful? Give feedback.
-
Summary
I've spent a good part of the weekend trying to figure out how to have a per-route middleware set up some state or extensions for a whole-route middleware.
Specifically, I'm working on a authorization framework, and I'd like to be able e.g. to fetch germane facts from the DB on a per-route basis, and then consider them in a whole-app policy. But from what I can tell, MethodRouter.layer() (or Handler.layer()) is always going to be inside the Router.layer() middleware, and so a Router middleware will consider the state before the Handler.layer() gets a chance to enrich it.
It seems like my best bet would be to move my Route middleware to a ServiceBuilder, and apply that to each MethodRouter by hand. I'm a little sad about that, with Route.layer() being right there.
axum version
0.7.5
Beta Was this translation helpful? Give feedback.
All reactions