-
Notifications
You must be signed in to change notification settings - Fork 6
/
probes.go
49 lines (40 loc) · 961 Bytes
/
probes.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
package provider
import (
"context"
"errors"
"net/http"
httphelper "github.com/zitadel/saml/pkg/http"
)
type ProbesFn func(context.Context) error
func healthHandler(w http.ResponseWriter, r *http.Request) {
ok(w)
}
func readyHandler(probes []ProbesFn) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
Readiness(w, r, probes...)
}
}
func Readiness(w http.ResponseWriter, r *http.Request, probes ...ProbesFn) {
ctx := r.Context()
for _, probe := range probes {
if err := probe(ctx); err != nil {
http.Error(w, "not ready", http.StatusInternalServerError)
return
}
}
ok(w)
}
func ReadyStorage(s Storage) ProbesFn {
return func(ctx context.Context) error {
if s == nil {
return errors.New("no storage")
}
return s.Health(ctx)
}
}
func ok(w http.ResponseWriter) {
httphelper.MarshalJSON(w, status{"ok"})
}
type status struct {
Status string `json:"status,omitempty"`
}