mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
feat(dashboard): introduce the in memory handler
the in memory handler gives etcd the ability to serve a dashboard without on disk resources. This is the first time we are using the /mod/ path too. TODO: cleanup the mod stuff so it isn't hacked into etcd_handlers.
This commit is contained in:
parent
637650e2a9
commit
431eebf754
@ -19,7 +19,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -42,7 +41,7 @@ func NewEtcdMuxer() *http.ServeMux {
|
|||||||
etcdMux.Handle("/"+version+"/stats/", errorHandler(StatsHttpHandler))
|
etcdMux.Handle("/"+version+"/stats/", errorHandler(StatsHttpHandler))
|
||||||
etcdMux.Handle("/version", errorHandler(VersionHttpHandler))
|
etcdMux.Handle("/version", errorHandler(VersionHttpHandler))
|
||||||
etcdMux.HandleFunc("/test/", TestHttpHandler)
|
etcdMux.HandleFunc("/test/", TestHttpHandler)
|
||||||
etcdMux.Handle("/dashboard/", DashboardHttpHandler())
|
etcdMux.Handle("/mod/dashboard/", DashboardHttpHandler("/mod/dashboard/"))
|
||||||
return etcdMux
|
return etcdMux
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,19 +277,6 @@ func GetHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DashboardHttpHandler either uses the compiled in virtual filesystem for the
|
|
||||||
// dashboard assets or if ETCD_DASHBOARD_DIR is set uses that as the source of
|
|
||||||
// assets.
|
|
||||||
func DashboardHttpHandler() http.Handler {
|
|
||||||
dashDir := os.Getenv("ETCD_DASHBOARD_DIR")
|
|
||||||
|
|
||||||
if len(dashDir) == 0 {
|
|
||||||
dashDir = "./"
|
|
||||||
}
|
|
||||||
|
|
||||||
return http.StripPrefix("/dashboard/", http.FileServer(http.Dir(dashDir)))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Watch handler
|
// Watch handler
|
||||||
func WatchHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
func WatchHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
||||||
key := req.URL.Path[len("/v1/watch/"):]
|
key := req.URL.Path[len("/v1/watch/"):]
|
||||||
|
42
etcd_modules.go
Normal file
42
etcd_modules.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/dashboard/resources"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DashboardMemoryFileServer(w http.ResponseWriter, req *http.Request) {
|
||||||
|
path := req.URL.Path
|
||||||
|
if len(path) == 0 {
|
||||||
|
path = "index.html"
|
||||||
|
}
|
||||||
|
|
||||||
|
b, ok := resources.File("/" + path)
|
||||||
|
|
||||||
|
if ok == false {
|
||||||
|
http.Error(w, path+": File not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.ServeContent(w, req, path, time.Time{}, bytes.NewReader(b))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// DashboardHttpHandler either uses the compiled in virtual filesystem for the
|
||||||
|
// dashboard assets or if ETCD_DASHBOARD_DIR is set uses that as the source of
|
||||||
|
// assets.
|
||||||
|
func DashboardHttpHandler(prefix string) (handler http.Handler) {
|
||||||
|
handler = http.HandlerFunc(DashboardMemoryFileServer)
|
||||||
|
|
||||||
|
// Serve the dashboard from a filesystem if the magic env variable is enabled
|
||||||
|
dashDir := os.Getenv("ETCD_DASHBOARD_DIR")
|
||||||
|
if len(dashDir) != 0 {
|
||||||
|
handler = http.FileServer(http.Dir(dashDir))
|
||||||
|
}
|
||||||
|
|
||||||
|
return http.StripPrefix(prefix, handler)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user