Giter VIP home page Giter VIP logo

Comments (6)

vishr avatar vishr commented on May 7, 2024 1

Now just e.Group servers both cases:
https://github.com/labstack/echo/blob/master/echo.go#L95

from echo.

vishr avatar vishr commented on May 7, 2024

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.

patrickdappollonio avatar patrickdappollonio commented on May 7, 2024

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.

jrobn avatar jrobn commented on May 7, 2024

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.

vishr avatar vishr commented on May 7, 2024

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.

patrickdappollonio avatar patrickdappollonio commented on May 7, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.