dashboard.go 971 B

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