feat: design a separate package for health check

This commit is contained in:
2026-02-15 11:56:12 +01:00
parent 8f30fe7412
commit a3ed6685de
5 changed files with 572 additions and 0 deletions

56
internal/health/health.go Normal file
View File

@@ -0,0 +1,56 @@
package health
import (
"context"
"time"
)
type Status string
const (
StatusHealthy Status = "healthy"
StatusDegraded Status = "degraded"
StatusUnhealthy Status = "unhealthy"
)
type Result struct {
Status Status `json:"status"`
Message string `json:"message,omitempty"`
Latency time.Duration `json:"latency"`
Timestamp time.Time `json:"timestamp"`
Details map[string]any `json:"details,omitempty"`
}
type Checker interface {
Name() string
Check(ctx context.Context) Result
}
type OverallResult struct {
Status Status `json:"status"`
Timestamp time.Time `json:"timestamp"`
Version string `json:"version"`
Services map[string]Result `json:"services"`
}
func determineOverallStatus(results map[string]Result) Status {
hasUnhealthy := false
hasDegraded := false
for _, result := range results {
switch result.Status {
case StatusUnhealthy:
hasUnhealthy = true
case StatusDegraded:
hasDegraded = true
}
}
if hasUnhealthy {
return StatusUnhealthy
}
if hasDegraded {
return StatusDegraded
}
return StatusHealthy
}