add healthcheck service

This commit is contained in:
2024-08-17 01:44:20 +03:00
parent 25762cbae8
commit f635d4ca5b
11 changed files with 422 additions and 15 deletions

View File

@ -5,6 +5,7 @@ import (
"net/http"
"net/http/pprof"
"git.loyso.art/frx/devsim/internal/probe"
"git.loyso.art/frx/devsim/internal/store"
)
@ -36,6 +37,11 @@ func (s *handlersBuilder) MountProfileHandlers() {
s.mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
func (s *handlersBuilder) MountProbeHandlers(r probe.Reporter) {
s.mux.HandleFunc("/health", livenessHandler(r))
s.mux.HandleFunc("/ready", readinessHandler(r))
}
func (s *handlersBuilder) Build() http.Handler {
return s.mux
}

View File

@ -0,0 +1,55 @@
package http
import (
"net/http"
"git.loyso.art/frx/devsim/internal/probe"
)
// ListenAndServe runs server to accept incoming connections. This function blocks on
// handling connections.
func livenessHandler(reporter probe.Reporter) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
switch reporter.ReportLiveness() {
case probe.LivenessOk:
w.WriteHeader(http.StatusOK)
case probe.LivenessTimeout:
w.WriteHeader(http.StatusInternalServerError)
case probe.LivenessUnknown:
w.WriteHeader(http.StatusOK)
}
})
}
func readinessHandler(reporter probe.Reporter) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
var status string
var code int
switch reporter.ReportReadiness() {
case probe.ReadinessOk:
status = "ok"
code = http.StatusOK
case probe.ReadinessFailed:
status = "failed"
code = http.StatusInternalServerError
case probe.ReadinessNotReady:
status = "not-ready"
code = http.StatusOK
case probe.ReadinessUnknown:
status = "unknown"
code = http.StatusOK
}
w.Header().Set("X-Readiness-Status", status)
w.WriteHeader(code)
})
}