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

Support "mounting" subrouters and route-sensitive middleware #4

Closed
aturon opened this issue Nov 8, 2018 · 3 comments
Closed

Support "mounting" subrouters and route-sensitive middleware #4

aturon opened this issue Nov 8, 2018 · 3 comments
Labels
design Open design question

Comments

@aturon
Copy link
Collaborator

aturon commented Nov 8, 2018

Right now, all routes and middleware must be set up at the top-level App. However, it's often useful to be able to nest an existing router (or potentially App at a given route inside another App. Similarly, it should be possible to set up middleware at specific nestings within a router, so that it only applies to paths with a specific prefix.

To sketch, this might look something like:

let mut app = App::new(my_data);
app.at("/foo").nest(|router| {
    router.middleware(some_middlware); // only applies to routes within this nesting
    router.at("bar").get(bar_endpoint); // ultimately routes `/foo/bar` to `bar_endpoint`
})

Let's nail down the API design and then work out a good implementation approach.

@aturon aturon added the design Open design question label Nov 8, 2018
@tirr-c
Copy link
Collaborator

tirr-c commented Nov 14, 2018

I came up with an idea, will try when I have enough time (maybe this weekend.)

Basic idea is,

  • Make Router::at public
  • Move middleware into Router
  • App::router(&mut self) will return top level router
  • Resource contains either a map of endpoints or a nested router
  • Resource::nest<F: FnOnce(&mut Router)>(&mut self, builder: F) will build a new Router and mount it

@tirr-c tirr-c mentioned this issue Nov 15, 2018
2 tasks
@J-F-Liu
Copy link

J-F-Liu commented Nov 15, 2018

I think the middleware design of Koa framework is quite good, can we do something similar in Rust?

app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx.set('X-Response-Time', `${ms}ms`);
});

@tirr-c
Copy link
Collaborator

tirr-c commented Nov 24, 2018

@J-F-Liu Seems like #54 is the thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Open design question
Projects
None yet
Development

No branches or pull requests

3 participants