Comments (6)
Now just e.Group servers both cases:
https://github.com/labstack/echo/blob/master/echo.go#L95
from echo.
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.
from echo.
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.
from echo.
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).
from echo.
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()
from echo.
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.
from echo.
Related Issues (20)
- Not able to detect the route url without \ HOT 8
- CSRF TokenLookup HOT 1
- Add an AllowContentType middleware (based on chi)
- Bind() panics when the destination is a map with non-interface types HOT 1
- Add SSE function in Context.Response HOT 7
- Any software design issues HOT 1
- Request support for more routing path matching HOT 1
- Unable to use proxy middleware along with bodyDump middleware HOT 1
- `Host` header always blank HOT 6
- Does echo support i18n? [feature request] HOT 2
- Weirdness with wrapped handler and embed FS (static files) HOT 1
- Remove default charset from 'application/json' Content-Type header
- echo v4 websocket client closed, program exited HOT 3
- router not setting ContextKeyHeaderAllow for group, causing cors preflight to not work HOT 6
- Static file has wrong body returned when using a custom HTTP Error Handler and complex paths HOT 2
- QueryParams seem to override formValues HOT 3
- DefaultHTTPErrorHandler does not log HOT 3
- Support Casbin Authorization Middleware HOT 1
- Echo response object calls flush on unflushable objects HOT 7
- Feature request: support Yaml responses transparently. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from echo.