zitadel-oidc/pkg/op/probes.go
2024-04-02 14:23:12 +03:00

49 lines
958 B
Go

package op
import (
"context"
"errors"
"net/http"
httphelper "github.com/zitadel/oidc/v4/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"`
}