feat: probes (#964)

* feat: probes

* feat: validate

* fix: protos

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
This commit is contained in:
Fabi
2020-11-20 10:09:17 +01:00
committed by GitHub
parent 7c6fd2b51e
commit 666e43a7e2
10 changed files with 21 additions and 201 deletions

View File

@@ -90,6 +90,7 @@ func (a *API) healthHandler() http.Handler {
handler := http.NewServeMux()
handler.HandleFunc("/healthz", handleHealth)
handler.HandleFunc("/ready", handleReadiness(checks))
handler.HandleFunc("/validate", handleValidate(checks))
handler.HandleFunc("/clientID", a.handleClientID)
return handler
@@ -102,12 +103,23 @@ func handleHealth(w http.ResponseWriter, r *http.Request) {
func handleReadiness(checks []ValidationFunction) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := validate(r.Context(), checks)
if err == nil {
errors := validate(r.Context(), checks)
if len(errors) == 0 {
http_util.MarshalJSON(w, "ok", nil, http.StatusOK)
return
}
http_util.MarshalJSON(w, nil, err, http.StatusPreconditionFailed)
http_util.MarshalJSON(w, nil, errors[0], http.StatusPreconditionFailed)
}
}
func handleValidate(checks []ValidationFunction) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
errors := validate(r.Context(), checks)
if len(errors) == 0 {
http_util.MarshalJSON(w, "ok", nil, http.StatusOK)
return
}
http_util.MarshalJSON(w, errors, nil, http.StatusOK)
}
}
@@ -122,12 +134,13 @@ func (a *API) handleClientID(w http.ResponseWriter, r *http.Request) {
type ValidationFunction func(ctx context.Context) error
func validate(ctx context.Context, validations []ValidationFunction) error {
func validate(ctx context.Context, validations []ValidationFunction) []error {
errors := make([]error, 0)
for _, validation := range validations {
if err := validation(ctx); err != nil {
logging.Log("API-vf823").WithError(err).WithField("traceID", tracing.TraceIDFromCtx(ctx)).Error("validation failed")
return err
errors = append(errors, err)
}
}
return nil
return errors
}