Tide: Support "scoped" middlewares

Created on 2 Sep 2019  路  4Comments  路  Source: http-rs/tide

Feature Request

In practices, we often want enable some middlewares on the subtree of the routes.

Detailed Description


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.

Context

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);
});

Possible Implementation

  1. Add associated middlewares information to the NFA states of route-recognizer.(Global middlewares associated with the initial state??)
  2. When perform the matching, we need to trace the path from the initial state to the terminal state. After the matching process, we can collect the associated middlewares on the path and invoke them in order.

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.

All 4 comments

Middleware can be divided into several situations: Root Level, (Nest|Scope|Group) LeveL, Route Level.

Maybe we need to consider a few points:

  1. nest routes (likes scope or group in other web frameworks) need to inherit the middleware of the parent?

  2. 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tirr-c picture tirr-c  路  6Comments

aturon picture aturon  路  6Comments

jbr picture jbr  路  4Comments

gruberb picture gruberb  路  4Comments

friktor picture friktor  路  3Comments