go: implement readiness library
This commit is contained in:
42
go-health/liveness.go
Normal file
42
go-health/liveness.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user