GAE Golang Gorilla mux - 404 page not found
Asked Answered
E

2

10

I've got some problems to use gorilla mux within GAE.

When I try it, I've '404 page not found'. The rootHandler function is not called ( no traces generated)

Below is part of my code, any ideas?

thk in advance

...
    func init() {
     r := mux.NewRouter()
     r.HandleFunc("/",rootHandler)
    }
    func rootHandler(w http.ResponseWriter, r *http.Request) {
     var functionName = "rootHandler"
     c := appengine.NewContext(r)
     c.Infof(functionName+"-start")
     defer c.Infof(functionName+"-end")
...
Eskimoaleut answered 29/12, 2012 at 11:9 Comment(0)
C
32

You have to route requests to your mux router. http package has DefaultServeMux which is used by AppEngine, but mux does not. (and it's not registering its routes with net/http by itself)

That is, all you have to do, is register your mux router with net/http:

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", HomeHandler)
    r.HandleFunc("/products", ProductsHandler)
    r.HandleFunc("/articles", ArticlesHandler)
    http.Handle("/", r)
}

(straight from the docs)

Important part here is http.Handle("/", r).

Clef answered 29/12, 2012 at 15:3 Comment(0)
S
2

You can also pass the router as the second argument to ListenAndServe since it implements the http.Handler interface.

router := mux.NewRouter()
router.HandleFunc("/", HomeHandler)
http.ListenAndServe(":8080", router) // pass the router here
Synthesis answered 13/12, 2021 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.