dashboard.go 1.3 KB

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