I’m using Go’s standard library and trying to register multiple HTTP handlers with http.NewServeMux. However, I’ve encountered an issue where only the route /reservation/cancel is registered, while other routes ending with / (like /) are not.
Here’s the code I’m using to register the handlers:
func (dep *ReservationHandler) Register() {
ware := middleware.RequestBodyMiddleware[model.ReservationPayload]{Logger: dep.logger}
mux := http.NewServeMux()
mux.HandleFunc("GET /", dep.availableDates)
mux.Handle("POST /", ware.RequestBody(http.HandlerFunc(dep.create)))
mux.HandleFunc("POST /cancel/{reservation_id}", dep.cancel)
dep.mux.Handle("/reservation/", http.StripPrefix("/reservation", mux))
}
Note that when I use the following approach, everything works as expected:
func (dep *ReservationHandler) Register() {
ware := middleware.RequestBodyMiddleware[model.ReservationPayload]{Logger: dep.logger}
dep.mux.HandleFunc("GET /reservation", dep.availableDates)
dep.mux.Handle("POST /reservation", ware.RequestBody(http.HandlerFunc(dep.create)))
dep.mux.HandleFunc("POST /reservation/cancel/{reservation_id}", dep.cancel)
}
Why is it that the routes with /
at the end aren’t registered in the first example, but the ones with explicit /reservation
work fine?