Merge pull request #4 from tbphp/fix-system-health-and-stats

fix: 监控检查及统计接口调整
This commit is contained in:
tbphp
2025-07-15 22:07:27 +08:00
committed by GitHub
3 changed files with 11 additions and 28 deletions

View File

@@ -6,7 +6,6 @@ import (
"time"
"gpt-load/internal/config"
"gpt-load/internal/models"
"gpt-load/internal/services"
"gpt-load/internal/types"
@@ -93,20 +92,6 @@ func (s *Server) Login(c *gin.Context) {
// Health handles health check requests
func (s *Server) Health(c *gin.Context) {
var totalKeys, healthyKeys int64
s.DB.Model(&models.APIKey{}).Count(&totalKeys)
s.DB.Model(&models.APIKey{}).Where("status = ?", models.KeyStatusActive).Count(&healthyKeys)
status := "healthy"
httpStatus := http.StatusOK
// Check if there are any healthy keys
if healthyKeys == 0 && totalKeys > 0 {
status = "unhealthy"
httpStatus = http.StatusServiceUnavailable
}
// Calculate uptime (this should be tracked from server start time)
uptime := "unknown"
if startTime, exists := c.Get("serverStartTime"); exists {
if st, ok := startTime.(time.Time); ok {
@@ -114,11 +99,9 @@ func (s *Server) Health(c *gin.Context) {
}
}
c.JSON(httpStatus, gin.H{
"status": status,
"timestamp": time.Now().UTC().Format(time.RFC3339),
"healthy_keys": healthyKeys,
"total_keys": totalKeys,
"uptime": uptime,
c.JSON(http.StatusOK, gin.H{
"status": "healthy",
"timestamp": time.Now().UTC().Format(time.RFC3339),
"uptime": uptime,
})
}

View File

@@ -6,11 +6,11 @@ import (
"strings"
"time"
"gpt-load/internal/response"
"gpt-load/internal/types"
"gpt-load/internal/channel"
"gpt-load/internal/services"
app_errors "gpt-load/internal/errors"
"gpt-load/internal/response"
"gpt-load/internal/services"
"gpt-load/internal/types"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
@@ -124,7 +124,7 @@ func Auth(
return func(c *gin.Context) {
path := c.Request.URL.Path
// Skip authentication for health/stats endpoints
// Skip authentication for health endpoints
if isMonitoringEndpoint(path) {
c.Next()
return
@@ -223,7 +223,7 @@ func ErrorHandler() gin.HandlerFunc {
// isMonitoringEndpoint checks if the path is a monitoring endpoint
func isMonitoringEndpoint(path string) bool {
monitoringPaths := []string{"/health", "/stats"}
monitoringPaths := []string{"/health"}
for _, monitoringPath := range monitoringPaths {
if path == monitoringPath {
return true

View File

@@ -57,8 +57,9 @@ func NewRouter(
router.Use(middleware.Logger(configManager.GetLogConfig()))
router.Use(middleware.CORS(configManager.GetCORSConfig()))
router.Use(middleware.RateLimiter(configManager.GetPerformanceConfig()))
startTime := time.Now()
router.Use(func(c *gin.Context) {
c.Set("serverStartTime", time.Now())
c.Set("serverStartTime", startTime)
c.Next()
})
@@ -74,7 +75,6 @@ func NewRouter(
// registerSystemRoutes 注册系统级路由
func registerSystemRoutes(router *gin.Engine, serverHandler *handler.Server) {
router.GET("/health", serverHandler.Health)
router.GET("/stats", serverHandler.Stats)
}
// registerAPIRoutes 注册API路由