Debian Codegraph @

pkg/op/probes.go

raw
1 package op
2
3 import (
4 "context"
5 "errors"
6 "net/http"
7
8 httphelper "github.com/zitadel/oidc/v3/pkg/http"
9 )
10
11 type ProbesFn func(context.Context) error
12
13 func healthHandler(w http.ResponseWriter, r *http.Request) {
14 ok(w)
15 }
16
17 func readyHandler(probes []ProbesFn) func(w http.ResponseWriter, r *http.Request) {
18 return func(w http.ResponseWriter, r *http.Request) {
19 Readiness(w, r, probes...)
20 }
21 }
22
23 func Readiness(w http.ResponseWriter, r *http.Request, probes ...ProbesFn) {
24 ctx := r.Context()
25 for _, probe := range probes {
26 if err := probe(ctx); err != nil {
27 http.Error(w, "not ready", http.StatusInternalServerError)
28 return
29 }
30 }
31 ok(w)
32 }
33
34 func ReadyStorage(s Storage) ProbesFn {
35 return func(ctx context.Context) error {
36 if s == nil {
37 return errors.New("no storage")
38 }
39 return s.Health(ctx)
40 }
41 }
42
43 func ok(w http.ResponseWriter) {
44 httphelper.MarshalJSON(w, Status{"ok"})
45 }
46
47 type Status struct {
48 Status string `json:"status,omitempty"`
49 }