dashboard.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package dashboard
  2. import (
  3. "bytes"
  4. "net/http"
  5. "os"
  6. "path"
  7. "time"
  8. "github.com/coreos/etcd/mod/dashboard/resources"
  9. )
  10. func memoryFileServer(w http.ResponseWriter, req *http.Request) {
  11. upath := req.URL.Path
  12. if len(upath) == 0 {
  13. upath = "index.html"
  14. }
  15. // TODO: use the new mux to do this work
  16. dir, file := path.Split(upath)
  17. if file == "browser" || file == "stats" {
  18. file = file + ".html"
  19. }
  20. upath = path.Join(dir, file)
  21. b, ok := resources.File("/" + upath)
  22. if ok == false {
  23. http.Error(w, upath+": File not found", http.StatusNotFound)
  24. return
  25. }
  26. http.ServeContent(w, req, upath, time.Time{}, bytes.NewReader(b))
  27. return
  28. }
  29. // DashboardHttpHandler either uses the compiled in virtual filesystem for the
  30. // dashboard assets or if ETCD_DASHBOARD_DIR is set uses that as the source of
  31. // assets.
  32. func HttpHandler() (handler http.Handler) {
  33. handler = http.HandlerFunc(memoryFileServer)
  34. // Serve the dashboard from a filesystem if the magic env variable is enabled
  35. dashDir := os.Getenv("ETCD_DASHBOARD_DIR")
  36. if len(dashDir) != 0 {
  37. handler = http.FileServer(http.Dir(dashDir))
  38. }
  39. return handler
  40. }