gogorillanegroni

Subrouter issues with negroni / gorilla mux


So I am trying to setup my router to respond to /users and /users/{userId} so I tried this code:

usersRouter := router.PathPrefix("/users").Subrouter()
usersRouter.HandleFunc("", users.GetUsersRoute).Methods("GET")
usersRouter.HandleFunc("/{userId:[0-9]*}", users.GetUserRoute).Methods("GET")

The issue is that I get a 404 error when I go to /users (but is does respond to /users/) If I do:

router.HandleFunc("/users", users.GetUsersRoute).Methods("GET")
router.HandleFunc("/users/{userId:[0-9]*}", users.GetUserRoute).Methods("GET")

It works like I want it to.

Is there any way to get the URLs to work like I want with Subrouters?


Solution

  • Yes and no. You can make the routes semi-work by adding StrictSlash(true) to the router.

    Given the following code

    package main
    
        import (
            "fmt"
            "net/http"
    
            "github.com/gorilla/mux"
        )
    
        func main() {
            mainRouter := mux.NewRouter().StrictSlash(true)
            mainRouter.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test") })
    
            subRouter := mainRouter.PathPrefix("/users").Subrouter()
            subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "/users") })
            subRouter.HandleFunc("/{id:[0-9]+}", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "/users/id") })
            http.ListenAndServe(":8080", mainRouter)
        }
    

    a request to http://localhost:8080/users will return

    < HTTP/1.1 301 Moved Permanently
    < Location: /users/
    < Date: Tue, 07 Apr 2015 19:52:12 GMT
    < Content-Length: 42
    < Content-Type: text/html; charset=utf-8
    < 
    <a href="/users/">Moved Permanently</a>.
    

    a request to http://localhost:8080/users/ returns

    < HTTP/1.1 200 OK
    < Date: Tue, 07 Apr 2015 19:54:43 GMT
    < Content-Length: 6
    < Content-Type: text/plain; charset=utf-8
    
    < /users
    

    so if your client is a browser then perhaps this is acceptable.