First of all, I've seen the thread
"Is it possible to nest Mux routers?", and unfortunately as the OP was so tied to Negroni no solution was posted, although I did take onboard some of the debugging tips.
I'm trying to do something like this, the contrived example is:
func NewUserRouter() *mux.Router {
userRouter := mux.NewRouter()
userRouter.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Users Index\n")
userRouter.HandleFunc("/show/{uuid}", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Users Show\n")
func NewOtherRouter() *mux.Router {
otherRouter := mux.NewRouter()
otherRouter.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Other Index\n")
r.Path("/users").Handler(NewUserRouter())
r.Path("/other").Handler(NewOtherRouter())
http.ListenAndServe(":3001", r)
This isn't the first thing I've tried, I've also played around unsuccessfully with `Path().Handler()` and `SubRouter()`, but to no avail, also with more liberal regular expressions, e.g. `r.Path("/users.*").Handler(NewUserRouter())`
I think it goes without saying, that the behaviour I'm expecting is to be able to do the following, with the following output:
$ curl localhost:3001/users
=> "Users Index"
$ curl localhost:3001/users/show/foo
$ curl localhost:3001/other
I made the following change to `gorilla/mux` to print some kind of debugging information, but it's not terribly helpful:
diff --git a/route.go b/route.go
index c310e66..8512b50 100644
@@ -35,12 +35,16 @@ type Route struct {
// Match matches the route against the request.
func (r *Route) Match(req *http.Request, match *RouteMatch) bool {
+ fmt.Printf("gorilla/mux Route.Match():\n\tRequest URL: %s\n", req.URL)
if r.buildOnly || r.err != nil {
for _, m := range r.matchers {
if matched := m.Match(req, match); !matched {
+ fmt.Printf("\tNot matched: %s\n", m)
It prints something like the following:
gorilla/mux Route.Match():
Not matched: &{/users %!s(bool=false) %!s(bool=false) %!s(bool=false) %!s(*regexp.Regexp=&{^/users$ 0xc208038b10 0xc208038b70 /users [47 117 115 101 114 115] false 47 8 4 0 [] false {0 0} [0xc208062600]}) /users [] []}
gorilla/mux Route.Match():
Not matched: &{/other %!s(bool=false) %!s(bool=false) %!s(bool=false) %!s(*regexp.Regexp=&{^/other$ 0xc208038e40 0xc208038ea0 /other [47 111 116 104 101 114] false 47 8 4 0 [] false {0 0} [0xc208062700]}) /other [] []}
(For simple curl requests to `curl localhost:3001/users`).
I wonder if someone can please bump me in the correct direction?