GAE Golang Gorilla mux - 404 page not found

我的未来我决定 提交于 2019-12-10 12:54:11

问题


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")
...

回答1:


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).



来源:https://stackoverflow.com/questions/14081066/gae-golang-gorilla-mux-404-page-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!