12 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
13 "github.com/Sirupsen/logrus"
14 "golang.org/x/net/websocket"
17 type wsConn interface {
19 Request() *http.Request
20 SetReadDeadline(time.Time) error
21 SetWriteDeadline(time.Time) error
26 eventSource eventSource
27 newPermChecker func() permChecker
36 status routerDebugStatus
39 type routerDebugStatus struct {
44 type debugStatuser interface {
45 DebugStatus() interface{}
48 func (rtr *router) setup() {
49 rtr.handler = &handler{
50 PingTimeout: rtr.Config.PingTimeout.Duration(),
51 QueueSize: rtr.Config.ClientEventQueue,
53 rtr.mux = http.NewServeMux()
54 rtr.mux.Handle("/websocket", rtr.makeServer(newSessionV0))
55 rtr.mux.Handle("/arvados/v1/events.ws", rtr.makeServer(newSessionV1))
56 rtr.mux.Handle("/debug.json", rtr.jsonHandler(rtr.DebugStatus))
57 rtr.mux.Handle("/status.json", rtr.jsonHandler(rtr.Status))
59 health := http.NewServeMux()
60 rtr.mux.Handle("/_health/", rtr.mgmtAuth(health))
61 health.Handle("/_health/ping", rtr.jsonHandler(rtr.HealthFunc(func() error { return nil })))
62 health.Handle("/_health/db", rtr.jsonHandler(rtr.HealthFunc(rtr.eventSource.DBHealth)))
65 func (rtr *router) makeServer(newSession sessionFactory) *websocket.Server {
66 return &websocket.Server{
67 Handshake: func(c *websocket.Config, r *http.Request) error {
70 Handler: websocket.Handler(func(ws *websocket.Conn) {
72 log := logger(ws.Request().Context())
75 stats := rtr.handler.Handle(ws, rtr.eventSource,
76 func(ws wsConn, sendq chan<- interface{}) (session, error) {
77 return newSession(ws, sendq, rtr.eventSource.DB(), rtr.newPermChecker(), &rtr.Config.Client)
80 log.WithFields(logrus.Fields{
81 "elapsed": time.Now().Sub(t0).Seconds(),
89 func (rtr *router) newReqID() string {
91 defer rtr.lastReqMtx.Unlock()
92 id := time.Now().UnixNano()
93 if id <= rtr.lastReqID {
94 id = rtr.lastReqID + 1
96 return strconv.FormatInt(id, 36)
99 func (rtr *router) DebugStatus() interface{} {
100 s := map[string]interface{}{
102 "Outgoing": rtr.handler.DebugStatus(),
104 if es, ok := rtr.eventSource.(debugStatuser); ok {
105 s["EventSource"] = es.DebugStatus()
110 var pingResponseOK = map[string]string{"health": "OK"}
112 func (rtr *router) HealthFunc(f func() error) func() interface{} {
113 return func() interface{} {
116 return pingResponseOK
118 return map[string]string{
120 "error": err.Error(),
125 func (rtr *router) Status() interface{} {
126 return map[string]interface{}{
127 "Clients": atomic.LoadInt64(&rtr.status.ReqsActive),
131 func (rtr *router) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
132 rtr.setupOnce.Do(rtr.setup)
133 atomic.AddInt64(&rtr.status.ReqsReceived, 1)
134 atomic.AddInt64(&rtr.status.ReqsActive, 1)
135 defer atomic.AddInt64(&rtr.status.ReqsActive, -1)
137 logger := logger(req.Context()).
138 WithField("RequestID", rtr.newReqID())
139 ctx := ctxlog.Context(req.Context(), logger)
140 req = req.WithContext(ctx)
141 logger.WithFields(logrus.Fields{
142 "remoteAddr": req.RemoteAddr,
143 "reqForwardedFor": req.Header.Get("X-Forwarded-For"),
144 }).Info("accept request")
145 rtr.mux.ServeHTTP(resp, req)
148 func (rtr *router) mgmtAuth(h http.Handler) http.Handler {
149 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
150 if rtr.Config.ManagementToken == "" {
151 http.Error(w, "disabled", http.StatusNotFound)
152 } else if ah := r.Header.Get("Authorization"); ah == "" {
153 http.Error(w, "authorization required", http.StatusUnauthorized)
154 } else if ah != "Bearer "+rtr.Config.ManagementToken {
155 http.Error(w, "authorization error", http.StatusForbidden)
162 func (rtr *router) jsonHandler(fn func() interface{}) http.Handler {
163 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
164 logger := logger(r.Context())
165 w.Header().Set("Content-Type", "application/json")
166 enc := json.NewEncoder(w)
167 err := enc.Encode(fn())
169 msg := "encode failed"
170 logger.WithError(err).Error(msg)
171 http.Error(w, msg, http.StatusInternalServerError)