From f3c2d169915bc78afbe8d356c169e91136c760ac Mon Sep 17 00:00:00 2001 From: Doflatango Date: Wed, 29 Nov 2017 10:14:11 +0800 Subject: [PATCH] handle 404 page: redirect to /ui/ --- web/routers.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/web/routers.go b/web/routers.go index 5981602..6ef7e62 100644 --- a/web/routers.go +++ b/web/routers.go @@ -104,6 +104,7 @@ func initRouters() (s *http.Server, err error) { subrouter.Handle("/configurations", h).Methods("GET") r.PathPrefix("/ui/").Handler(http.StripPrefix("/ui/", newEmbeddedFileServer("", "index.html"))) + r.NotFoundHandler = NewBaseHandler(notFoundHandler) s = &http.Server{ Handler: r, @@ -146,6 +147,37 @@ func (s *embeddedFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - w.WriteHeader(http.StatusNotFound) - w.Write([]byte("404 page not found")) + _notFoundHandler(w, r) +} + +func notFoundHandler(c *Context) { + _notFoundHandler(c.W, c.R) +} + +func _notFoundHandler(w http.ResponseWriter, r *http.Request) { + const html = ` + + + + 404 page not found + + + The page you are looking for is not found. Redirect to Dashboard after 5 seconds. + + +` + + if strings.HasPrefix(strings.TrimLeft(r.URL.Path, "/"), "v1") { + outJSONWithCode(w, http.StatusNotFound, "Api not found.") + } else { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(html)) + } }