In practices, we often want enable some middlewares on the subtree of the routes.
For example, we have URI /v1/:user_id/posts/:post_id, and we need to check whether the post belongs to that user. Or we have an OAuth2Middleware, but the only subset of our APIs need to be protected.
Currently, we need to intrusive modify the middleware if we want to archive this purpose. We have the maintain the router but also the middleware implementation.
root.at("/:user_id/posts").nest(|parent| {
parent.middleware(access_guard); // <----
parent.post(crate::service::post::new_post);
parent.at("/:post_id")
.get(crate::service::post::get_post)
.put(crate::service::post::mod_post)
.delete(crate::service::post::del_post);
});
route-recognizer.(Global middlewares associated with the initial state??)Middleware can be divided into several situations: Root Level, (Nest|Scope|Group) LeveL, Route Level.
Maybe we need to consider a few points:
nest routes (likes scope or group in other web frameworks) need to inherit the middleware of the parent?
need to clear middleware of the parent for nest routes?
It was supported before, but removed during revamp. Original commit: https://github.com/rustasync/tide/commit/dfe72abe238ad7afd642c4b546596393bd1bdade
Is there any plan to bring this feature back?
Also, the article linked in the README is misleading, as it shows an example of the common use-case scenario of protecting a subset of routes: https://rustasync.github.io/team/2018/11/27/tide-middleware-evolution.html
Maybe a comment about it not being currently supported should be added.
@kamyuentse @cquintana-verbio I once implemented router nesting and per-endpoint middleware, and I definitely want to re-implement and get this back! I just need a clear design that can be documented or written in code.
A simple solution of per-endpoint middleware would be an endpoint wrapper. It would wrap an existing endpoint and apply middleware when invoked. Router nesting is rather tough; it should deal with partial matching and collecting match params.
Most helpful comment
@kamyuentse @cquintana-verbio I once implemented router nesting and per-endpoint middleware, and I definitely want to re-implement and get this back! I just need a clear design that can be documented or written in code.
A simple solution of per-endpoint middleware would be an endpoint wrapper. It would wrap an existing endpoint and apply middleware when invoked. Router nesting is rather tough; it should deal with partial matching and collecting match params.