Browse Source

feat(dashboard): add sigle-page-app html5 pushstate support

Ed Rooth 11 years ago
parent
commit
77270c6b00
2 changed files with 15 additions and 2 deletions
  1. 12 1
      mod/dashboard/dashboard.go
  2. 3 1
      mod/mod.go

+ 12 - 1
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
+}

+ 3 - 1
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)))