Skip to content

Commit a0abea1

Browse files
authored
Merge pull request #4 from tbphp/fix-system-health-and-stats
fix: 监控检查及统计接口调整
2 parents 06525f3 + 98d436d commit a0abea1

File tree

3 files changed

+11
-28
lines changed

3 files changed

+11
-28
lines changed

internal/handler/handler.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"gpt-load/internal/config"
9-
"gpt-load/internal/models"
109
"gpt-load/internal/services"
1110
"gpt-load/internal/types"
1211

@@ -93,32 +92,16 @@ func (s *Server) Login(c *gin.Context) {
9392

9493
// Health handles health check requests
9594
func (s *Server) Health(c *gin.Context) {
96-
var totalKeys, healthyKeys int64
97-
s.DB.Model(&models.APIKey{}).Count(&totalKeys)
98-
s.DB.Model(&models.APIKey{}).Where("status = ?", models.KeyStatusActive).Count(&healthyKeys)
99-
100-
status := "healthy"
101-
httpStatus := http.StatusOK
102-
103-
// Check if there are any healthy keys
104-
if healthyKeys == 0 && totalKeys > 0 {
105-
status = "unhealthy"
106-
httpStatus = http.StatusServiceUnavailable
107-
}
108-
109-
// Calculate uptime (this should be tracked from server start time)
11095
uptime := "unknown"
11196
if startTime, exists := c.Get("serverStartTime"); exists {
11297
if st, ok := startTime.(time.Time); ok {
11398
uptime = time.Since(st).String()
11499
}
115100
}
116101

117-
c.JSON(httpStatus, gin.H{
118-
"status": status,
119-
"timestamp": time.Now().UTC().Format(time.RFC3339),
120-
"healthy_keys": healthyKeys,
121-
"total_keys": totalKeys,
122-
"uptime": uptime,
102+
c.JSON(http.StatusOK, gin.H{
103+
"status": "healthy",
104+
"timestamp": time.Now().UTC().Format(time.RFC3339),
105+
"uptime": uptime,
123106
})
124107
}

internal/middleware/middleware.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
"strings"
77
"time"
88

9-
"gpt-load/internal/response"
10-
"gpt-load/internal/types"
119
"gpt-load/internal/channel"
12-
"gpt-load/internal/services"
1310
app_errors "gpt-load/internal/errors"
11+
"gpt-load/internal/response"
12+
"gpt-load/internal/services"
13+
"gpt-load/internal/types"
1414

1515
"github.com/gin-gonic/gin"
1616
"github.com/sirupsen/logrus"
@@ -124,7 +124,7 @@ func Auth(
124124
return func(c *gin.Context) {
125125
path := c.Request.URL.Path
126126

127-
// Skip authentication for health/stats endpoints
127+
// Skip authentication for health endpoints
128128
if isMonitoringEndpoint(path) {
129129
c.Next()
130130
return
@@ -223,7 +223,7 @@ func ErrorHandler() gin.HandlerFunc {
223223

224224
// isMonitoringEndpoint checks if the path is a monitoring endpoint
225225
func isMonitoringEndpoint(path string) bool {
226-
monitoringPaths := []string{"/health", "/stats"}
226+
monitoringPaths := []string{"/health"}
227227
for _, monitoringPath := range monitoringPaths {
228228
if path == monitoringPath {
229229
return true

internal/router/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ func NewRouter(
5757
router.Use(middleware.Logger(configManager.GetLogConfig()))
5858
router.Use(middleware.CORS(configManager.GetCORSConfig()))
5959
router.Use(middleware.RateLimiter(configManager.GetPerformanceConfig()))
60+
startTime := time.Now()
6061
router.Use(func(c *gin.Context) {
61-
c.Set("serverStartTime", time.Now())
62+
c.Set("serverStartTime", startTime)
6263
c.Next()
6364
})
6465

@@ -74,7 +75,6 @@ func NewRouter(
7475
// registerSystemRoutes 注册系统级路由
7576
func registerSystemRoutes(router *gin.Engine, serverHandler *handler.Server) {
7677
router.GET("/health", serverHandler.Health)
77-
router.GET("/stats", serverHandler.Stats)
7878
}
7979

8080
// registerAPIRoutes 注册API路由

0 commit comments

Comments
 (0)