zitadel-oidc/pkg/op/probes.go
Livio Amstutz 0ab5ea5a57 refactor: remove utils pkg
BREAKING CHANGE: utils package has been removed in favor of specific new
packages (http, crypto, strings)
2021-09-27 11:58:28 +02:00

57 lines
1.1 KiB
Go

package op
import (
"context"
"errors"
"net/http"
httphelper "github.com/caos/oidc/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 ReadySigner(s Signer) ProbesFn {
return func(ctx context.Context) error {
if s == nil {
return errors.New("no signer")
}
return s.Health(ctx)
}
}
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"`
}