dashboard.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. b, err := resources.Asset(req.URL.Path)
  15. if err != nil {
  16. http.Error(w, upath+": File not found", http.StatusNotFound)
  17. return
  18. }
  19. http.ServeContent(w, req, upath, time.Time{}, bytes.NewReader(b))
  20. return
  21. }
  22. func getDashDir() string {
  23. return os.Getenv("ETCD_DASHBOARD_DIR")
  24. }
  25. // DashboardHttpHandler either uses the compiled in virtual filesystem for the
  26. // dashboard assets or if ETCD_DASHBOARD_DIR is set uses that as the source of
  27. // assets.
  28. func HttpHandler() (handler http.Handler) {
  29. handler = http.HandlerFunc(memoryFileServer)
  30. // Serve the dashboard from a filesystem if the magic env variable is enabled
  31. dashDir := getDashDir()
  32. if len(dashDir) != 0 {
  33. log.Debugf("Using dashboard directory %s", dashDir)
  34. handler = http.FileServer(http.Dir(dashDir))
  35. }
  36. return handler
  37. }
  38. // Always returns the index.html page.
  39. func IndexPage(w http.ResponseWriter, req *http.Request) {
  40. dashDir := getDashDir()
  41. if len(dashDir) != 0 {
  42. // Serve the index page from disk if the env variable is set.
  43. http.ServeFile(w, req, path.Join(dashDir, "index.html"))
  44. } else {
  45. // Otherwise serve it from the compiled resources.
  46. b, _ := resources.Asset("index.html")
  47. http.ServeContent(w, req, "index.html", time.Time{}, bytes.NewReader(b))
  48. }
  49. return
  50. }