X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/060d38d627bd1e51dd2b3c6e7de9af6aa7d7b6f3..443a0b96316ed46600dc5035193adae6ac4d1f74:/services/ws/router.go diff --git a/services/ws/router.go b/services/ws/router.go index 3f3a051d8e..a408b58bdd 100644 --- a/services/ws/router.go +++ b/services/ws/router.go @@ -14,7 +14,8 @@ import ( "time" "git.curoverse.com/arvados.git/sdk/go/ctxlog" - "github.com/Sirupsen/logrus" + "git.curoverse.com/arvados.git/sdk/go/health" + "github.com/sirupsen/logrus" "golang.org/x/net/websocket" ) @@ -60,10 +61,18 @@ func (rtr *router) setup() { rtr.mux.Handle("/debug.json", rtr.jsonHandler(rtr.DebugStatus)) rtr.mux.Handle("/status.json", rtr.jsonHandler(rtr.Status)) - health := http.NewServeMux() - rtr.mux.Handle("/_health/", rtr.mgmtAuth(health)) - health.Handle("/_health/ping", rtr.jsonHandler(rtr.HealthFunc(func() error { return nil }))) - health.Handle("/_health/db", rtr.jsonHandler(rtr.HealthFunc(rtr.eventSource.DBHealth))) + rtr.mux.Handle("/_health/", &health.Handler{ + Token: rtr.Config.ManagementToken, + Prefix: "/_health/", + Routes: health.Routes{ + "db": rtr.eventSource.DBHealth, + }, + Log: func(r *http.Request, err error) { + if err != nil { + logger(r.Context()).WithError(err).Error("error") + } + }, + }) } func (rtr *router) makeServer(newSession sessionFactory) *websocket.Server { @@ -111,24 +120,10 @@ func (rtr *router) DebugStatus() interface{} { return s } -var pingResponseOK = map[string]string{"health": "OK"} - -func (rtr *router) HealthFunc(f func() error) func() interface{} { - return func() interface{} { - err := f() - if err == nil { - return pingResponseOK - } - return map[string]string{ - "health": "ERROR", - "error": err.Error(), - } - } -} - func (rtr *router) Status() interface{} { return map[string]interface{}{ "Clients": atomic.LoadInt64(&rtr.status.ReqsActive), + "Version": version, } } @@ -149,20 +144,6 @@ func (rtr *router) ServeHTTP(resp http.ResponseWriter, req *http.Request) { rtr.mux.ServeHTTP(resp, req) } -func (rtr *router) mgmtAuth(h http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if rtr.Config.ManagementToken == "" { - http.Error(w, "disabled", http.StatusNotFound) - } else if ah := r.Header.Get("Authorization"); ah == "" { - http.Error(w, "authorization required", http.StatusUnauthorized) - } else if ah != "Bearer "+rtr.Config.ManagementToken { - http.Error(w, "authorization error", http.StatusForbidden) - } else { - h.ServeHTTP(w, r) - } - }) -} - func (rtr *router) jsonHandler(fn func() interface{}) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { logger := logger(r.Context())