Per-route middlewares
This should be an enhancement. It is useful for a few of us out here that a router + middleware would allow per-route middlewares. Say you have a small admin panel and you want to use the http-auth middleware in just that route, but you don't want to apply it to all the other routes declared, currently, looks like it's not possible without something like alice.
Several routers already have the option to mix with certain middleware chainers such as alice or negroni, but they always loose any kind of functionality related to the router, though. For example, you can create an alice chain to a certain handler in httprouter but then, how do you retrieve any named parameter from the url? By passing the handler to alice, you lose the ability to retrieve them in the handler.
Thanks for your feedback. I have this in plan but wanted more detailed discussion before implementing.
In the example below, we have a couple of choices /admin:
- inherit all middleware from parent and execute in this order
mw1 > mw2 > mw3 > mw4 > h3 - don't inherit any middleware from parent and executes in this order
mw4 > h3
e := echo.New()
e.Use(mw1, mw2, mw3)
e.Get("/users", h1)
e.Get("/users/:1", h2)
// Sub router
a := e.Sub("/admin")
a.Use(mw4)
a.Get("", h3)
Let me know your thoughts.
I don't know clearly. What I could see from other routers + middleware combinations is that you first append site-wide routes and then you take care of the special routes. That means mw1 > mw2 > mw3 > mw4 > h3.
This produces obviously another problem: what if I want to chain a middleware to the stack and then I want to remove it? From what I saw, it's just a matter of be well organized and those combinations don't usually allows to "abandon" a middleware. That said, there are a few routers + middleware who also have an e.Abandon(mw1) who actually removes a middleware from the chain. But then, the solution brings more problems to the table than before.
So, in my opinion, should be executing all middlewares (both the global ones and the per-route ones) and see from there if the community needs an abandon method, a way to remove them. I guess if that happens, there should be then a better way to do what the user wants.
I would take the later of the two options of not inheriting any middleware from parent. This gives maximum flexibility without (imho) the added complexity of adding e.Abadon() to the API. I would also change the method to e.Group() instead, since e.Sub() implies inherited hierarchy of some kind.
e := echo.New()
e.Use(mw1, mw2, mw3)
a := e.Group("/admin")
a.Use(mw1, mw2, mw3) // same stack
// or
a.Use(mw1, mw3, mw4) // combo of any middleware
Or you could offer both an e.Sub() (inherited middelware) and an e.Group() (no middleware inheritance).
Thank you all for your feedback. I am almost ready with the implementation, just need few more inputs to close on this.
I was thinking of e.Sub(inherit bool) which also solves for e.Group()
I like the idea of e.Sub() (as full inheritance) and e.Group() (as no inheritance). Adding a boolean just gave me the feel that the result will not be verbose.
Now just e.Group servers both cases: https://github.com/labstack/echo/blob/master/echo.go#L95