chi icon indicating copy to clipboard operation
chi copied to clipboard

Feature: Include router option that prevents over writing routes

Open djensen47 opened this issue 3 years ago • 5 comments

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

djensen47 avatar Feb 03 '23 22:02 djensen47

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.

pkieltyka avatar Feb 05 '23 15:02 pkieltyka

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: @.***>

djensen47 avatar Feb 05 '23 17:02 djensen47

@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).

bubbajoe avatar Feb 13 '23 01:02 bubbajoe

@pkieltyka Is there still a plan to add this?

bubbajoe avatar Sep 15 '23 13:09 bubbajoe

hi, this is not planned

pkieltyka avatar Sep 15 '23 13:09 pkieltyka