From 77270c6b0064915697c75818b0a510a01ad8389c Mon Sep 17 00:00:00 2001 From: Ed Rooth Date: Thu, 10 Apr 2014 10:17:25 -0700 Subject: [PATCH] feat(dashboard): add sigle-page-app html5 pushstate support --- mod/dashboard/dashboard.go | 13 ++++++++++++- mod/mod.go | 4 +++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/mod/dashboard/dashboard.go b/mod/dashboard/dashboard.go index c462c382b..e64b3b3cc 100644 --- a/mod/dashboard/dashboard.go +++ b/mod/dashboard/dashboard.go @@ -35,6 +35,10 @@ func memoryFileServer(w http.ResponseWriter, req *http.Request) { return } +func getDashDir() string { + return os.Getenv("ETCD_DASHBOARD_DIR") +} + // DashboardHttpHandler either uses the compiled in virtual filesystem for the // dashboard assets or if ETCD_DASHBOARD_DIR is set uses that as the source of // assets. @@ -42,7 +46,7 @@ func HttpHandler() (handler http.Handler) { handler = http.HandlerFunc(memoryFileServer) // Serve the dashboard from a filesystem if the magic env variable is enabled - dashDir := os.Getenv("ETCD_DASHBOARD_DIR") + dashDir := getDashDir() if len(dashDir) != 0 { log.Debugf("Using dashboard directory %s", dashDir) handler = http.FileServer(http.Dir(dashDir)) @@ -50,3 +54,10 @@ func HttpHandler() (handler http.Handler) { return handler } + +// Always returns the index.html page. +func IndexPage(w http.ResponseWriter, req *http.Request) { + dashDir := getDashDir() + http.ServeFile(w, req, path.Join(dashDir, "index.html")) + return +} diff --git a/mod/mod.go b/mod/mod.go index 1b1002794..59358a0c9 100644 --- a/mod/mod.go +++ b/mod/mod.go @@ -21,7 +21,9 @@ func addSlash(w http.ResponseWriter, req *http.Request) { func HttpHandler(addr string) http.Handler { r := mux.NewRouter() r.HandleFunc("/dashboard", addSlash) - r.PathPrefix("/dashboard/").Handler(http.StripPrefix("/dashboard/", dashboard.HttpHandler())) + + r.PathPrefix("/dashboard/static/").Handler(http.StripPrefix("/dashboard/static/", dashboard.HttpHandler())) + r.HandleFunc("/dashboard{path:.*}", dashboard.IndexPage) r.PathPrefix("/v2/lock").Handler(http.StripPrefix("/v2/lock", lock2.NewHandler(addr))) r.PathPrefix("/v2/leader").Handler(http.StripPrefix("/v2/leader", leader2.NewHandler(addr)))