完善本地质量门禁与资金链路测试

This commit is contained in:
yml2213
2026-09-02 13:50:19 +08:00
parent dd2b45bd2c
commit 208f12ddb4
80 changed files with 12245 additions and 11541 deletions
+44 -3
View File
@@ -1,7 +1,9 @@
package handler
import (
"context"
"net/http"
"sort"
"time"
"github.com/gin-gonic/gin"
@@ -9,16 +11,55 @@ import (
type HealthHandler struct {
startedAt time.Time
checks map[string]ReadinessCheck
}
func NewHealthHandler() *HealthHandler {
return &HealthHandler{startedAt: time.Now()}
type ReadinessCheck func(context.Context) error
func NewHealthHandler(checks map[string]ReadinessCheck) *HealthHandler {
return &HealthHandler{startedAt: time.Now(), checks: checks}
}
func (h *HealthHandler) Check(c *gin.Context) {
func (h *HealthHandler) Live(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "ok",
"service": "hfb-api",
"started_at": h.startedAt.Format(time.RFC3339),
})
}
func (h *HealthHandler) Ready(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
defer cancel()
names := make([]string, 0, len(h.checks))
for name := range h.checks {
names = append(names, name)
}
sort.Strings(names)
ready := true
dependencies := make(map[string]string, len(names))
for _, name := range names {
check := h.checks[name]
if check == nil || check(ctx) != nil {
dependencies[name] = "unavailable"
ready = false
continue
}
dependencies[name] = "ok"
}
statusCode := http.StatusOK
status := "ok"
if !ready {
statusCode = http.StatusServiceUnavailable
status = "unavailable"
}
c.JSON(statusCode, gin.H{
"status": status,
"service": "hfb-api",
"started_at": h.startedAt.Format(time.RFC3339),
"dependencies": dependencies,
})
}