Nested Gorilla Mux router does not work
Asked Answered
L

4

10

Using code below, when I access /test2 it responds with 404 - not found. /test1 works correctly. Why is that? Is nesting not allowed despite the fact that routers implement http.Handler interface?

package main

import (
    "fmt"
    "net/http"
    "github.com/gorilla/mux"
)

func main() {

    mainRouter := mux.NewRouter()
    subRouter := mux.NewRouter()

    mainRouter.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test1") })

    subRouter.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test2") })

    mainRouter.Handle("/", subRouter)

    http.ListenAndServe(":9999", mainRouter)
}

EDIT:

My main goal was to add some initial work which would be common for all routes in subRouter, and only for them. To be even more specific, I would like to use Negroni as my middleware orchiestrator. On the Negroni website there is an example of adding middleware to the group of routes:

router := mux.NewRouter()
adminRoutes := mux.NewRouter()
// add admin routes here

Create a new negroni for the admin middleware
router.Handle("/admin", negroni.New(
  Middleware1, 
  Middleware2, 
  negroni.Wrap(adminRoutes),
)) 

Negroni basically executes ServeHTTP methods of every argument, since all of them implement http.Handler. It executes them in order, so router routes will be last.

I'm familiar with the concept of Subrouter in Mux, but AFAIK I can't use it in similar fashion as example above, in particular, I can't inject anything between mainRouter and its Subrouter. This is why nesting looks more flexible.

Lafond answered 3/8, 2014 at 18:26 Comment(0)
W
12

I know this question is somewhat old, but I have spent some time figuring out how handlers and matching work in go. You can see my experiment code here.

Basically, you can get the effect you want with code like this:

package main

import (
    "fmt"
    "net/http"
    "github.com/gorilla/mux"
)

func main() {

    mainRouter := mux.NewRouter()
    subRouter := mux.NewRouter()

    mainRouter.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "test1")
    })
    subRouter.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "test2")
    })

    // in mux, you need to register subrouter
    // with the same path that the handlers in
    // it are matching
    mainRouter.Handle("/test2", subRouter)

    // if your subrouter has handlers that match
    // other sub paths - you also need to do this
    mainRouter.Handle("/test2/{_dummy:.*}", subRouter)

    http.ListenAndServe(":9999", mainRouter)
}

I hope this helps someone.

Wadai answered 5/12, 2014 at 22:7 Comment(1)
so even thous http.ListenAndServe is called with mainRouter, subRouter will also be included?Frit
N
8

None of the previous answers given helped me achieve exactly what I was seeking out to do. I was trying to use negroni.Wrap() around a Subrouter with lots of routes. I believe that is what the original poster wanted.

Additional Context: I am deploying on Google App Engine, hence putting everything in the init() function.

package hello

import (
    "fmt"
    "net/http"

    "github.com/codegangsta/negroni"
    "github.com/gorilla/mux"
)

func init() {
    // Create the "root" router, if you will...
    r := mux.NewRouter().StrictSlash(true)

    // Create your "Subrouter" dedicated to /api which will use the PathPrefix
    apiRouter := mux.NewRouter().PathPrefix("/api").Subrouter().StrictSlash(true)

    // This step is where we connect our "root" router and our "Subrouter" together.
    r.PathPrefix("/api").Handler(negroni.New(
        negroni.HandlerFunc(myMiddleware),
        negroni.Wrap(apiRouter),
    ))

    // Define "root" routes using r
    r.HandleFunc(genHandleFunc("/", "root of site"))
    r.HandleFunc(genHandleFunc("/home", "home"))

    // Define "Subrouter" routes using apiRouter, prefix is /api
    apiRouter.HandleFunc(genHandleFunc("/", "root of API, /api"))                                       // Matches: /api
    apiRouter.HandleFunc(genHandleFunc("/v1", "root of API V1, /api/v1"))                               // Matches: /api/v1
    apiRouter.HandleFunc(genHandleFunc("/v1/resourceabc", "API V1 - resourceabc, /api/v1/resourceabc")) // Matches: /api/v1/resourceabc

    /* Finally we pass our "root" router to the net/http library. The "root" router will contain all
    of the routes for /api also.
    */
    http.Handle("/", r)
}

// Silly function to quickly generate a HandleFunc
func genHandleFunc(p string, msg string) (path string, f func(http.ResponseWriter, *http.Request)) {
    path = p
    f = func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "%v\n", msg)
    }
    return
}

func myMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
    fmt.Fprintln(w, "Before doing work...")
    next(w, r)
    fmt.Fprintln(w, "After doing work...")
}
Newel answered 29/5, 2015 at 19:53 Comment(0)
M
7

You wouldn't use two routers here anyway.

Gorilla Mux has the concept of a Subrouter, whereby you define the top level domain properties on the main router, then use the Subrouter instance to map the individual paths.

For example:

mainRouter := mux.NewRouter()
subRouter := mainRouter.PathPrefix("/").Subrouter()

subRouter.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test1") })
subRouter.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test2") })

mainRouter.Handle("/", mainRouter)

You can go even further than that though - for example, you may have another router at /test1 and a subrouter that matches anything further below that (say /test1/othertest).

Manuscript answered 3/8, 2014 at 23:0 Comment(2)
I have edited my question to be more specific about what I wanted to achieve. I think Subrouter will not solve the case.Lafond
would be nice if you demonstrate the other half of the example: /test1/othertestPase
Y
4

Use full path in subroutes:

router := mux.NewRouter()

apiRoutes := mux.NewRouter()
apiRoutes.Handle("/api/auth", Auth)

router.PathPrefix("/api").Handler(negroni.New(
  Middleware1,
  Middleware2,
  negroni.Wrap(apiRoutes),
)) 
Yttriferous answered 1/3, 2015 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.