Files
devsim/internal/api/http/healthhandlers.go

56 lines
1.4 KiB
Go

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)
})
}