Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go and Gorilla Mux NotFoundHandler not working

Tags:

go

mux

gorilla

I just can't get this NotFoundHandler to work. I'd like to serve a static file on every get request, given that it exists, otherwise serve index.html. Here's my simplified router at the moment:

func fooHandler() http.Handler {
  fn := func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Foo"))
  }
  return http.HandlerFunc(fn)
}

func notFound(w http.ResponseWriter, r *http.Request) {
  http.ServeFile(w, r, "public/index.html")
}

func main() {
  router = mux.NewRouter()
  fs := http.FileServer(http.Dir("public"))

  router.Handle("/foo", fooHandler())
  router.PathPrefix("/").Handler(fs)
  router.NotFoundHandler = http.HandlerFunc(notFound)

  http.ListenAndServe(":3000", router)
}

/foo works ok

/file-that-exists works ok

/file-that-doesnt-exist doesn't work - I get 404 page not found instead of index.html

So what am I doing wrong here?

like image 740
QlliOlli Avatar asked Oct 26 '14 13:10

QlliOlli


2 Answers

The problem is that router.PathPrefix("/").Handler(fs) will match every route and the NotFoundHandler is never executed. The NotFoundHandler is only executed when the router can't find a matching route.

When you explicitly define your routes it works as expected.

You could do something like:

router.Handle("/foo", fooHandler())
router.PathPrefix("/assets").Handler(fs)
router.HandleFunc("/", index)
router.HandleFunc("/about", about)
router.HandleFunc("/contact", contact)
router.NotFoundHandler = http.HandlerFunc(notFound)
like image 86
lukad Avatar answered Nov 18 '22 20:11

lukad


This has worked for me

r.NotFoundHandler = http.HandlerFunc(NotFound)

Makesure your 'NotFound' function has:

func NotFound(w http.ResponseWriter, r *http.Request) { // a * before http.Request
like image 34
Tyrese Avatar answered Nov 18 '22 20:11

Tyrese