go: implement readiness library

This commit is contained in:
Elijah Duffy
2026-01-27 18:13:33 -08:00
parent a73105586b
commit 0ab0168a8d
2 changed files with 263 additions and 0 deletions

42
go-health/liveness.go Normal file
View File

@@ -0,0 +1,42 @@
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)
}
}