Files
health/go-health/liveness.go
2026-01-27 18:13:33 -08:00

43 lines
988 B
Go

package health
import (
"encoding/json"
"net/http"
"time"
)
// Status of the service liveness.
type LivenessStatus string
const (
// Service is alive and functioning properly.
LivenessStatusAlive LivenessStatus = "ok"
)
// Result of a liveness check.
type LivenessResult struct {
// Status of the service (always LivenessStatusAlive)
Status LivenessStatus
// Timestamp of the liveness check in milliseconds since Unix epoch
Timestamp int64
}
// Checks if the service is alive.
func Liveness() LivenessResult {
return LivenessResult{
Status: LivenessStatusAlive,
Timestamp: time.Now().UnixMilli(),
}
}
// Handler for liveness HTTP requests.
// Returns a response with status 200 and a JSON body matching LivenessResult.
func LivenessHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
result := Liveness()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(result)
}
}