优化代码格式

This commit is contained in:
user123456
2025-07-27 10:58:20 +08:00
parent 1881b5b1ba
commit 07a926902a
12 changed files with 94 additions and 111 deletions

View File

@@ -44,7 +44,6 @@ require (
github.com/vbatts/tar-split v0.12.1 // indirect github.com/vbatts/tar-split v0.12.1 // indirect
golang.org/x/arch v0.8.0 // indirect golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.32.0 // indirect golang.org/x/crypto v0.32.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sync v0.14.0 // indirect golang.org/x/sync v0.14.0 // indirect
golang.org/x/sys v0.33.0 // indirect golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.21.0 // indirect golang.org/x/text v0.21.0 // indirect

View File

@@ -155,17 +155,7 @@ func main() {
} }
} }
// 简单的健康检查 // 简单的健康检查
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 { func formatDuration(d time.Duration) string {
if d < time.Minute { if d < time.Minute {
return fmt.Sprintf("%d秒", int(d.Seconds())) return fmt.Sprintf("%d秒", int(d.Seconds()))
@@ -180,26 +170,20 @@ func formatDuration(d time.Duration) string {
} }
} }
func initHealthRoutes(router *gin.Engine) { func getUptimeInfo() (time.Duration, float64, string) {
router.GET("/health", func(c *gin.Context) {
uptime := time.Since(serviceStartTime) uptime := time.Since(serviceStartTime)
c.JSON(http.StatusOK, gin.H{ return uptime, uptime.Seconds(), formatDuration(uptime)
"status": "healthy", }
"timestamp_unix": serviceStartTime.Unix(),
"uptime_sec": uptime.Seconds(),
"service": "hubproxy",
"start_time_bj": formatBeijingTime(serviceStartTime),
"uptime_human": formatDuration(uptime),
})
})
func initHealthRoutes(router *gin.Engine) {
router.GET("/ready", func(c *gin.Context) { router.GET("/ready", func(c *gin.Context) {
uptime := time.Since(serviceStartTime) _, uptimeSec, uptimeHuman := getUptimeInfo()
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"ready": true, "ready": true,
"timestamp_unix": time.Now().Unix(), "service": "hubproxy",
"uptime_sec": uptime.Seconds(), "start_time_unix": serviceStartTime.Unix(),
"uptime_human": formatDuration(uptime), "uptime_sec": uptimeSec,
"uptime_human": uptimeHuman,
}) })
}) })
} }