Browse Source

feat(dashboard): introduce the in memory handler

the in memory handler gives etcd the ability to serve a dashboard
without on disk resources. This is the first time we are using the
/mod/ path too.

TODO: cleanup the mod stuff so it isn't hacked into etcd_handlers.
Brandon Philips 12 years ago
parent
commit
8acaf71a59
1 changed files with 42 additions and 0 deletions
  1. 42 0
      etcd_modules.go

+ 42 - 0
etcd_modules.go

@@ -0,0 +1,42 @@
+package main
+
+import (
+	"bytes"
+	"net/http"
+	"os"
+	"time"
+
+	"github.com/coreos/etcd/dashboard/resources"
+)
+
+func DashboardMemoryFileServer(w http.ResponseWriter, req *http.Request) {
+	path := req.URL.Path
+	if len(path) == 0 {
+		path = "index.html"
+	}
+
+	b, ok := resources.File("/" + path)
+
+	if ok == false {
+		http.Error(w, path+": File not found", http.StatusNotFound)
+		return
+	}
+
+	http.ServeContent(w, req, path, time.Time{}, bytes.NewReader(b))
+	return
+}
+
+// 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.
+func DashboardHttpHandler(prefix string) (handler http.Handler) {
+	handler = http.HandlerFunc(DashboardMemoryFileServer)
+
+	// Serve the dashboard from a filesystem if the magic env variable is enabled
+	dashDir := os.Getenv("ETCD_DASHBOARD_DIR")
+	if len(dashDir) != 0 {
+		handler = http.FileServer(http.Dir(dashDir))
+	}
+
+	return http.StripPrefix(prefix, handler)
+}