From fa14a5f5be1be67fe9438213b184c180d5af8cce Mon Sep 17 00:00:00 2001 From: user123456 Date: Fri, 13 Jun 2025 12:06:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=81=A5=E5=BA=B7=E7=9B=91?= =?UTF-8?q?=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/main.go b/src/main.go index e7fcc66..7273baa 100644 --- a/src/main.go +++ b/src/main.go @@ -9,6 +9,7 @@ import ( "regexp" "strconv" "strings" + "time" "github.com/gin-gonic/gin" ) @@ -44,6 +45,9 @@ var ( regexp.MustCompile(`^(?:https?://)?(github|opengraph)\.githubassets\.com/([^/]+)/.+?$`), } globalLimiter *IPRateLimiter + + // ✅ 服务启动时间追踪 + serviceStartTime = time.Now() ) func main() { @@ -77,6 +81,9 @@ func main() { }) })) + // ✅ 初始化监控端点 (优先级最高,避免中间件影响) + initHealthRoutes(router) + // 初始化镜像tar下载路由 initImageTarRoutes(router) @@ -307,3 +314,74 @@ func checkURL(u string) []string { } return nil } + +// ✅ 初始化健康监控路由 +func initHealthRoutes(router *gin.Engine) { + // 健康检查端点 - 最轻量级,无依赖检查 + router.GET("/health", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "status": "healthy", + "timestamp": time.Now().Unix(), + "uptime": time.Since(serviceStartTime).Seconds(), + "service": "hubproxy", + }) + }) + + // 就绪检查端点 - 检查关键组件状态 + router.GET("/ready", func(c *gin.Context) { + checks := make(map[string]string) + allReady := true + + // 检查配置状态 + if GetConfig() != nil { + checks["config"] = "ok" + } else { + checks["config"] = "failed" + allReady = false + } + + // 检查全局缓存状态 + if globalCache != nil { + checks["cache"] = "ok" + } else { + checks["cache"] = "failed" + allReady = false + } + + // 检查限流器状态 + if globalLimiter != nil { + checks["ratelimiter"] = "ok" + } else { + checks["ratelimiter"] = "failed" + allReady = false + } + + // 检查镜像下载器状态 + if globalImageStreamer != nil { + checks["imagestreamer"] = "ok" + } else { + checks["imagestreamer"] = "failed" + allReady = false + } + + // 检查HTTP客户端状态 + if GetGlobalHTTPClient() != nil { + checks["httpclient"] = "ok" + } else { + checks["httpclient"] = "failed" + allReady = false + } + + status := http.StatusOK + if !allReady { + status = http.StatusServiceUnavailable + } + + c.JSON(status, gin.H{ + "ready": allReady, + "checks": checks, + "timestamp": time.Now().Unix(), + "uptime": time.Since(serviceStartTime).Seconds(), + }) + }) +}