github.com/argoproj/argo-cd@v1.8.7/util/healthz/healthz.go (about)

     1  package healthz
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  // ServeHealthCheck serves the health check endpoint.
    11  // ServeHealthCheck relies on the provided function to return an error if unhealthy and nil otherwise.
    12  func ServeHealthCheck(mux *http.ServeMux, f func(r *http.Request) error) {
    13  	mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
    14  		if err := f(r); err != nil {
    15  			w.WriteHeader(http.StatusServiceUnavailable)
    16  			log.Errorln(w, err)
    17  		} else {
    18  			fmt.Fprintln(w, "ok")
    19  		}
    20  	})
    21  }