dashboard.go 1.1 KB

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