Skip to content

Commit 208a239

Browse files
authored
修复cf导致的协议头问题,简化健康检查
1 parent 1fb97b5 commit 208a239

File tree

1 file changed

+48
-64
lines changed

1 file changed

+48
-64
lines changed

src/main.go

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,18 @@ func handler(c *gin.Context) {
143143
for strings.HasPrefix(rawPath, "/") {
144144
rawPath = strings.TrimPrefix(rawPath, "/")
145145
}
146-
147-
if !strings.HasPrefix(rawPath, "http") {
148-
c.String(http.StatusForbidden, "无效输入")
149-
return
146+
// 自动补全协议头
147+
if !strings.HasPrefix(rawPath, "https://") {
148+
// 修复 http:/ 和 https:/ 的情况
149+
if strings.HasPrefix(rawPath, "http:/") || strings.HasPrefix(rawPath, "https:/") {
150+
rawPath = strings.Replace(rawPath, "http:/", "", 1)
151+
rawPath = strings.Replace(rawPath, "https:/", "", 1)
152+
} else if strings.HasPrefix(rawPath, "http://") {
153+
rawPath = strings.TrimPrefix(rawPath, "http://")
154+
}
155+
rawPath = "https://" + rawPath
150156
}
151-
157+
152158
matches := checkURL(rawPath)
153159
if matches != nil {
154160
// GitHub仓库访问控制检查
@@ -308,72 +314,50 @@ func checkURL(u string) []string {
308314
return nil
309315
}
310316

311-
// 初始化健康监控路由
317+
// 简单的健康检查
318+
func formatBeijingTime(t time.Time) string {
319+
loc, err := time.LoadLocation("Asia/Shanghai")
320+
if err != nil {
321+
loc = time.FixedZone("CST", 8*3600) // 兜底时区
322+
}
323+
return t.In(loc).Format("2006-01-02 15:04:05")
324+
}
325+
326+
// 转换为可读时间
327+
func formatDuration(d time.Duration) string {
328+
if d < time.Minute {
329+
return fmt.Sprintf("%d秒", int(d.Seconds()))
330+
} else if d < time.Hour {
331+
return fmt.Sprintf("%d分钟%d秒", int(d.Minutes()), int(d.Seconds())%60)
332+
} else if d < 24*time.Hour {
333+
return fmt.Sprintf("%d小时%d分钟", int(d.Hours()), int(d.Minutes())%60)
334+
} else {
335+
days := int(d.Hours()) / 24
336+
hours := int(d.Hours()) % 24
337+
return fmt.Sprintf("%d天%d小时", days, hours)
338+
}
339+
}
340+
312341
func initHealthRoutes(router *gin.Engine) {
313-
// 健康检查端点
314342
router.GET("/health", func(c *gin.Context) {
343+
uptime := time.Since(serviceStartTime)
315344
c.JSON(http.StatusOK, gin.H{
316-
"status": "healthy",
317-
"timestamp": time.Now().Unix(),
318-
"uptime": time.Since(serviceStartTime).Seconds(),
319-
"service": "hubproxy",
345+
"status": "healthy",
346+
"timestamp_unix": serviceStartTime.Unix(),
347+
"uptime_sec": uptime.Seconds(),
348+
"service": "hubproxy",
349+
"start_time_bj": formatBeijingTime(serviceStartTime),
350+
"uptime_human": formatDuration(uptime),
320351
})
321352
})
322353

323-
// 就绪检查端点
324354
router.GET("/ready", func(c *gin.Context) {
325-
checks := make(map[string]string)
326-
allReady := true
327-
328-
if GetConfig() != nil {
329-
checks["config"] = "ok"
330-
} else {
331-
checks["config"] = "failed"
332-
allReady = false
333-
}
334-
335-
// 检查全局缓存状态
336-
if globalCache != nil {
337-
checks["cache"] = "ok"
338-
} else {
339-
checks["cache"] = "failed"
340-
allReady = false
341-
}
342-
343-
// 检查限流器状态
344-
if globalLimiter != nil {
345-
checks["ratelimiter"] = "ok"
346-
} else {
347-
checks["ratelimiter"] = "failed"
348-
allReady = false
349-
}
350-
351-
// 检查镜像下载器状态
352-
if globalImageStreamer != nil {
353-
checks["imagestreamer"] = "ok"
354-
} else {
355-
checks["imagestreamer"] = "failed"
356-
allReady = false
357-
}
358-
359-
// 检查HTTP客户端状态
360-
if GetGlobalHTTPClient() != nil {
361-
checks["httpclient"] = "ok"
362-
} else {
363-
checks["httpclient"] = "failed"
364-
allReady = false
365-
}
366-
367-
status := http.StatusOK
368-
if !allReady {
369-
status = http.StatusServiceUnavailable
370-
}
371-
372-
c.JSON(status, gin.H{
373-
"ready": allReady,
374-
"checks": checks,
375-
"timestamp": time.Now().Unix(),
376-
"uptime": time.Since(serviceStartTime).Seconds(),
355+
uptime := time.Since(serviceStartTime)
356+
c.JSON(http.StatusOK, gin.H{
357+
"ready": true,
358+
"timestamp_unix": time.Now().Unix(),
359+
"uptime_sec": uptime.Seconds(),
360+
"uptime_human": formatDuration(uptime),
377361
})
378362
})
379363
}

0 commit comments

Comments
 (0)