Adds a simple healthcheck every 2s (#1814)

* Adds a simple healthcheck every 5s

* Fixes bugs for healthcheck
This commit is contained in:
Amir Raminfar
2022-07-20 11:56:42 -07:00
committed by GitHub
parent 93f57b6e90
commit 5cffa287d5
5 changed files with 64 additions and 0 deletions

View File

@@ -61,6 +61,7 @@ func createRouter(h *handler) *mux.Router {
s.HandleFunc("/api/validateCredentials", h.validateCredentials)
s.Handle("/logout", authorizationRequired(h.clearSession))
s.Handle("/version", authorizationRequired(h.version))
s.HandleFunc("/healthcheck", h.healthcheck)
if log.IsLevelEnabled(log.DebugLevel) {
s.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux)
@@ -131,3 +132,14 @@ func (h *handler) version(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html")
fmt.Fprintf(w, "<pre>%v</pre>", h.config.Version)
}
func (h *handler) healthcheck(w http.ResponseWriter, r *http.Request) {
log.Trace("Executing healthcheck request")
if ping, err := h.client.Ping(r.Context()); err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
fmt.Fprintf(w, "OK API Version %v", ping.APIVersion)
}
}