package http import ( "fmt" "net/http" "edge-infra.dev/pkg/lib/fog" dsHTTP "edge-infra.dev/pkg/edge/datasync/http" "edge-infra.dev/pkg/edge/datasync/internal/config" ) var logger = fog.New() func NewLivenessServer(shouldRun chan bool, cfg *config.Config) *dsHTTP.Server { serverMux := http.NewServeMux() serverMux.HandleFunc("/healthz", healthz) return dsHTTP.NewServer(cfg.LivenessPort, serverMux, shouldRun) } func healthz(w http.ResponseWriter, _ *http.Request) { isAlive := isServerAlive() if isAlive { fmt.Fprintf(w, "up") return } w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "down") } // isServerAlive performs a check to ensure the server is still in a healthy and useable state func isServerAlive() bool { // TODO: Find a better way to check for "healthyness" than just validating config // over and over. Config should only need to be validated once on startup anyways since it doesn't change. err := config.ValidateConfiguration() if err != nil { logger.Error(err, "Liveness check failed") } return err == nil }