/
streamStatus.go
42 lines (35 loc) · 971 Bytes
/
streamStatus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package api
import (
"context"
"errors"
"net/http"
"github.com/livepeer/livepeer-data/health"
"github.com/livepeer/livepeer-data/pkg/data"
)
type contextKey int
const (
streamStatusKey contextKey = iota
)
func streamStatus(healthcore *health.Core) middleware {
return inlineMiddleware(func(rw http.ResponseWriter, r *http.Request, next http.Handler) {
if healthcore == nil {
respondError(rw, http.StatusNotImplemented, errors.New("stream healthcore is unavailable"))
return
}
streamID := apiParam(r, streamIDParam)
if streamID == "" {
next.ServeHTTP(rw, r)
return
}
status, err := healthcore.GetStatus(streamID)
if err != nil {
respondError(rw, http.StatusInternalServerError, err)
return
}
r = r.WithContext(context.WithValue(r.Context(), streamStatusKey, status))
next.ServeHTTP(rw, r)
})
}
func getStreamStatus(r *http.Request) *data.HealthStatus {
return r.Context().Value(streamStatusKey).(*data.HealthStatus)
}