Feature: Include router option that prevents over writing routes
I discovered a scenario where you can create two handlers that handle the same pattern but the second one "overwrites" the first one.
The expected behavior would be to panic. Because the pattern GET /things being attempted to be added twice
baseRouter := chi.NewRouter()
baseRouter.Get("/things", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("things"))
})
things := chi.NewRouter()
things.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("other things"))
})
baseRouter.Mount("/things", things) //should panic
The example below I would not expect to panic because there are no overlapping URL patterns. The are GET /things and GET /things/other
baseRouter := chi.NewRouter()
baseRouter.Get("/things", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("things"))
})
things := chi.NewRouter()
things.Get("/other", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("other things"))
})
baseRouter.Mount("/things", things) //should not panic
For a GET /things the response will be other things
hi there, so chi does allow overwriting routes. I understand it can be a bit confusing when this happens unintentionally, but, it's very useful when you need it. Perhaps we add some kind of "Options" to a router, and we could specify if we want to allow overwriting routes or not, and if not, then would panic.
Yes, having that option would be fantastic!
On Sun, Feb 5, 2023 at 7:59 AM Peter Kieltyka @.***> wrote:
hi there, so chi does allow overwriting routes. I understand it can be a bit confusing when this happens unintentionally, but, it's very useful when you need it. Perhaps we add some kind of "Options" to a router, and we could specify if we want to allow overwriting routes or not, and if not, then would panic.
— Reply to this email directly, view it on GitHub https://github.com/go-chi/chi/issues/792#issuecomment-1418081322, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEA36MIFUXXK37JMIURYRTWV7E4PANCNFSM6AAAAAAUQYHFUE . You are receiving this because you authored the thread.Message ID: @.***>
@pkieltyka I was looking for something similar!
+1 for this, but in addition to adding an option for panicking, how about a separate function similar to Match(rctx *Context, method, path string) bool, like MatchPattern(rctx *Context, method, pattern string) (*Route, bool)?
I use chi as a reverse proxy that dynamically adds routes, and i would like to check if that particular route/pattern is already taken (and which one if possible).
@pkieltyka Is there still a plan to add this?
hi, this is not planned