So I want to get rid of one more dependency (gorilla/mux) and use the std http.ServeMux
with it's new routing patterns.
The problem is, that gorilla/mux had this concept of a subrouter, which could handle everything under a specified path.
I needed to serve paths like:
GET /api/user/id
POST /api/user/id
GET /api/product/id
So I had something like:
apirouter := mainRouter.PathPrefix("/api").Subrouter()
users.RegisterHandler(apirouter)
products.RegisterHandler(apirouter)
and then in eg. users
package:
func RegisterHandler(router *mux.Router) {
subrouter := router.PathPrefix("/user").Subrouter()
subrouter.HandleFunc("/{id}", getUser).Methods(http.MethodGet)
subrouter.HandleFunc("/{id}", postUser).Methods(http.MethodPost)
subrouter.HandleFunc("/{id}", updateUser).Methods(http.MethodPut)
}
How could this be achieved with http.ServeMux
? I want to be able to create a subrouter, pass it around and register new routes under it.
Something like this?
func RegisterHandler(router *http.ServeMux) {
subrouter := http.NewServeMux()
router.Handle("/user", someMagicMiddleware(subrouter))
subrouter.HandleFunc("GET /{id}", getUser)
subrouter.HandleFunc("POST /{id}", postUser)
subrouter.HandleFunc("PUT /{id}", updateUser)
}
Ok so I think I have it:
It creates a new
http.ServeMux
which handles all subsequent requests under that prefix. And the sub-paths can be registered independently :)users
package:Any enhancements welcome :)