修复cf导致的协议头问题,简化健康检查
This commit is contained in:
110
src/main.go
110
src/main.go
@@ -143,10 +143,16 @@ func handler(c *gin.Context) {
|
|||||||
for strings.HasPrefix(rawPath, "/") {
|
for strings.HasPrefix(rawPath, "/") {
|
||||||
rawPath = strings.TrimPrefix(rawPath, "/")
|
rawPath = strings.TrimPrefix(rawPath, "/")
|
||||||
}
|
}
|
||||||
|
// 自动补全协议头
|
||||||
if !strings.HasPrefix(rawPath, "http") {
|
if !strings.HasPrefix(rawPath, "https://") {
|
||||||
c.String(http.StatusForbidden, "无效输入")
|
// 修复 http:/ 和 https:/ 的情况
|
||||||
return
|
if strings.HasPrefix(rawPath, "http:/") || strings.HasPrefix(rawPath, "https:/") {
|
||||||
|
rawPath = strings.Replace(rawPath, "http:/", "", 1)
|
||||||
|
rawPath = strings.Replace(rawPath, "https:/", "", 1)
|
||||||
|
} else if strings.HasPrefix(rawPath, "http://") {
|
||||||
|
rawPath = strings.TrimPrefix(rawPath, "http://")
|
||||||
|
}
|
||||||
|
rawPath = "https://" + rawPath
|
||||||
}
|
}
|
||||||
|
|
||||||
matches := checkURL(rawPath)
|
matches := checkURL(rawPath)
|
||||||
@@ -308,72 +314,50 @@ func checkURL(u string) []string {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化健康监控路由
|
// 简单的健康检查
|
||||||
|
func formatBeijingTime(t time.Time) string {
|
||||||
|
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||||
|
if err != nil {
|
||||||
|
loc = time.FixedZone("CST", 8*3600) // 兜底时区
|
||||||
|
}
|
||||||
|
return t.In(loc).Format("2006-01-02 15:04:05")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转换为可读时间
|
||||||
|
func formatDuration(d time.Duration) string {
|
||||||
|
if d < time.Minute {
|
||||||
|
return fmt.Sprintf("%d秒", int(d.Seconds()))
|
||||||
|
} else if d < time.Hour {
|
||||||
|
return fmt.Sprintf("%d分钟%d秒", int(d.Minutes()), int(d.Seconds())%60)
|
||||||
|
} else if d < 24*time.Hour {
|
||||||
|
return fmt.Sprintf("%d小时%d分钟", int(d.Hours()), int(d.Minutes())%60)
|
||||||
|
} else {
|
||||||
|
days := int(d.Hours()) / 24
|
||||||
|
hours := int(d.Hours()) % 24
|
||||||
|
return fmt.Sprintf("%d天%d小时", days, hours)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func initHealthRoutes(router *gin.Engine) {
|
func initHealthRoutes(router *gin.Engine) {
|
||||||
// 健康检查端点
|
|
||||||
router.GET("/health", func(c *gin.Context) {
|
router.GET("/health", func(c *gin.Context) {
|
||||||
|
uptime := time.Since(serviceStartTime)
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"status": "healthy",
|
"status": "healthy",
|
||||||
"timestamp": time.Now().Unix(),
|
"timestamp_unix": serviceStartTime.Unix(),
|
||||||
"uptime": time.Since(serviceStartTime).Seconds(),
|
"uptime_sec": uptime.Seconds(),
|
||||||
"service": "hubproxy",
|
"service": "hubproxy",
|
||||||
|
"start_time_bj": formatBeijingTime(serviceStartTime),
|
||||||
|
"uptime_human": formatDuration(uptime),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// 就绪检查端点
|
|
||||||
router.GET("/ready", func(c *gin.Context) {
|
router.GET("/ready", func(c *gin.Context) {
|
||||||
checks := make(map[string]string)
|
uptime := time.Since(serviceStartTime)
|
||||||
allReady := true
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"ready": true,
|
||||||
if GetConfig() != nil {
|
"timestamp_unix": time.Now().Unix(),
|
||||||
checks["config"] = "ok"
|
"uptime_sec": uptime.Seconds(),
|
||||||
} else {
|
"uptime_human": formatDuration(uptime),
|
||||||
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(),
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user