forked from quay/clair
/
indexstatehandler.go
54 lines (46 loc) · 1.22 KB
/
indexstatehandler.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
43
44
45
46
47
48
49
50
51
52
53
54
package httptransport
import (
"net/http"
je "github.com/quay/claircore/pkg/jsonerr"
"github.com/quay/clair/v4/indexer"
"github.com/quay/clair/v4/internal/codec"
)
// IndexStateHandler utilizes a Stater to report the
// curent runtime state of an Indexer.
//
// Indexers running with different scanner versions
// will produce unique states and indicate to clients
// a re-index is necessary.
func IndexStateHandler(service indexer.Stater) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
ctx := r.Context()
s, err := service.State(ctx)
if err != nil {
resp := &je.Response{
Code: "internal error",
Message: "could not retrieve indexer state " + err.Error(),
}
je.Error(w, resp, http.StatusInternalServerError)
return
}
tag := `"` + s + `"`
w.Header().Add("etag", tag)
if unmodified(r, tag) {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Set("content-type", "application/json")
defer writerError(w, &err)()
enc := codec.GetEncoder(w)
defer codec.PutEncoder(enc)
err = enc.Encode(struct {
State string `json:"state"`
}{
State: s,
})
}
}