14324: Use logrus in Azure driver. Fix Sirupsen->sirupsen in imports
[arvados.git] / services / ws / router.go
index 15b825f2abfa293f8ba2734f57508ba9306a03af..a408b58bddf31b5483f799c779823cdfcd98902d 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 package main
 
 import (
@@ -10,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"
 )
 
@@ -53,8 +58,21 @@ func (rtr *router) setup() {
        rtr.mux = http.NewServeMux()
        rtr.mux.Handle("/websocket", rtr.makeServer(newSessionV0))
        rtr.mux.Handle("/arvados/v1/events.ws", rtr.makeServer(newSessionV1))
-       rtr.mux.HandleFunc("/debug.json", jsonHandler(rtr.DebugStatus))
-       rtr.mux.HandleFunc("/status.json", jsonHandler(rtr.Status))
+       rtr.mux.Handle("/debug.json", rtr.jsonHandler(rtr.DebugStatus))
+       rtr.mux.Handle("/status.json", rtr.jsonHandler(rtr.Status))
+
+       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 {
@@ -105,6 +123,7 @@ func (rtr *router) DebugStatus() interface{} {
 func (rtr *router) Status() interface{} {
        return map[string]interface{}{
                "Clients": atomic.LoadInt64(&rtr.status.ReqsActive),
+               "Version": version,
        }
 }
 
@@ -125,16 +144,16 @@ func (rtr *router) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
        rtr.mux.ServeHTTP(resp, req)
 }
 
-func jsonHandler(fn func() interface{}) http.HandlerFunc {
-       return func(resp http.ResponseWriter, req *http.Request) {
-               logger := logger(req.Context())
-               resp.Header().Set("Content-Type", "application/json")
-               enc := json.NewEncoder(resp)
+func (rtr *router) jsonHandler(fn func() interface{}) http.Handler {
+       return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               logger := logger(r.Context())
+               w.Header().Set("Content-Type", "application/json")
+               enc := json.NewEncoder(w)
                err := enc.Encode(fn())
                if err != nil {
                        msg := "encode failed"
                        logger.WithError(err).Error(msg)
-                       http.Error(resp, msg, http.StatusInternalServerError)
+                       http.Error(w, msg, http.StatusInternalServerError)
                }
-       }
+       })
 }