feat: 基础框架搭建完毕
Checks / verify (push) Failing after 2m37s

This commit is contained in:
2026-10-07 09:27:47 +08:00
parent ba6dfffab7
commit bffdb3858b
64 changed files with 326 additions and 332 deletions
+49
View File
@@ -0,0 +1,49 @@
package service
import (
"context"
"sync"
"time"
)
type Probe func(context.Context) error
type Component struct {
Status string `json:"status"`
}
type Health struct {
Status string `json:"status"`
CheckedAt time.Time `json:"checked_at"`
Components map[string]Component `json:"components"`
}
type HealthService struct{ postgres, redis Probe }
func NewHealthService(postgres, redis Probe) *HealthService {
return &HealthService{postgres: postgres, redis: redis}
}
func (s *HealthService) Check(ctx context.Context) Health {
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
result := Health{Status: "ready", CheckedAt: time.Now().UTC(), Components: map[string]Component{}}
var mu sync.Mutex
var wg sync.WaitGroup
for name, probe := range map[string]Probe{"postgresql": s.postgres, "redis": s.redis} {
wg.Go(func() {
status := "unavailable"
if probe == nil {
status = "not_configured"
} else if probe(ctx) == nil {
status = "ready"
}
mu.Lock()
defer mu.Unlock()
result.Components[name] = Component{Status: status}
if status != "ready" {
result.Status = "not_ready"
}
})
}
wg.Wait()
return result
}