etcd_modules.go 993 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package main
  2. import (
  3. "bytes"
  4. "net/http"
  5. "os"
  6. "time"
  7. "github.com/coreos/etcd/dashboard/resources"
  8. )
  9. func DashboardMemoryFileServer(w http.ResponseWriter, req *http.Request) {
  10. path := req.URL.Path
  11. if len(path) == 0 {
  12. path = "index.html"
  13. }
  14. b, ok := resources.File("/" + path)
  15. if ok == false {
  16. http.Error(w, path+": File not found", http.StatusNotFound)
  17. return
  18. }
  19. http.ServeContent(w, req, path, time.Time{}, bytes.NewReader(b))
  20. return
  21. }
  22. // DashboardHttpHandler either uses the compiled in virtual filesystem for the
  23. // dashboard assets or if ETCD_DASHBOARD_DIR is set uses that as the source of
  24. // assets.
  25. func DashboardHttpHandler(prefix string) (handler http.Handler) {
  26. handler = http.HandlerFunc(DashboardMemoryFileServer)
  27. // Serve the dashboard from a filesystem if the magic env variable is enabled
  28. dashDir := os.Getenv("ETCD_DASHBOARD_DIR")
  29. if len(dashDir) != 0 {
  30. handler = http.FileServer(http.Dir(dashDir))
  31. }
  32. return http.StripPrefix(prefix, handler)
  33. }