fix: health and stats
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gpt-load/internal/config"
|
"gpt-load/internal/config"
|
||||||
"gpt-load/internal/models"
|
|
||||||
"gpt-load/internal/services"
|
"gpt-load/internal/services"
|
||||||
"gpt-load/internal/types"
|
"gpt-load/internal/types"
|
||||||
|
|
||||||
@@ -93,20 +92,6 @@ func (s *Server) Login(c *gin.Context) {
|
|||||||
|
|
||||||
// Health handles health check requests
|
// Health handles health check requests
|
||||||
func (s *Server) Health(c *gin.Context) {
|
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"
|
uptime := "unknown"
|
||||||
if startTime, exists := c.Get("serverStartTime"); exists {
|
if startTime, exists := c.Get("serverStartTime"); exists {
|
||||||
if st, ok := startTime.(time.Time); ok {
|
if st, ok := startTime.(time.Time); ok {
|
||||||
@@ -114,11 +99,9 @@ func (s *Server) Health(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(httpStatus, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"status": status,
|
"status": "healthy",
|
||||||
"timestamp": time.Now().UTC().Format(time.RFC3339),
|
"timestamp": time.Now().UTC().Format(time.RFC3339),
|
||||||
"healthy_keys": healthyKeys,
|
"uptime": uptime,
|
||||||
"total_keys": totalKeys,
|
|
||||||
"uptime": uptime,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -6,11 +6,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gpt-load/internal/response"
|
|
||||||
"gpt-load/internal/types"
|
|
||||||
"gpt-load/internal/channel"
|
"gpt-load/internal/channel"
|
||||||
"gpt-load/internal/services"
|
|
||||||
app_errors "gpt-load/internal/errors"
|
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/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@@ -124,7 +124,7 @@ func Auth(
|
|||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
path := c.Request.URL.Path
|
path := c.Request.URL.Path
|
||||||
|
|
||||||
// Skip authentication for health/stats endpoints
|
// Skip authentication for health endpoints
|
||||||
if isMonitoringEndpoint(path) {
|
if isMonitoringEndpoint(path) {
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
@@ -223,7 +223,7 @@ func ErrorHandler() gin.HandlerFunc {
|
|||||||
|
|
||||||
// isMonitoringEndpoint checks if the path is a monitoring endpoint
|
// isMonitoringEndpoint checks if the path is a monitoring endpoint
|
||||||
func isMonitoringEndpoint(path string) bool {
|
func isMonitoringEndpoint(path string) bool {
|
||||||
monitoringPaths := []string{"/health", "/stats"}
|
monitoringPaths := []string{"/health"}
|
||||||
for _, monitoringPath := range monitoringPaths {
|
for _, monitoringPath := range monitoringPaths {
|
||||||
if path == monitoringPath {
|
if path == monitoringPath {
|
||||||
return true
|
return true
|
||||||
|
@@ -57,8 +57,9 @@ func NewRouter(
|
|||||||
router.Use(middleware.Logger(configManager.GetLogConfig()))
|
router.Use(middleware.Logger(configManager.GetLogConfig()))
|
||||||
router.Use(middleware.CORS(configManager.GetCORSConfig()))
|
router.Use(middleware.CORS(configManager.GetCORSConfig()))
|
||||||
router.Use(middleware.RateLimiter(configManager.GetPerformanceConfig()))
|
router.Use(middleware.RateLimiter(configManager.GetPerformanceConfig()))
|
||||||
|
startTime := time.Now()
|
||||||
router.Use(func(c *gin.Context) {
|
router.Use(func(c *gin.Context) {
|
||||||
c.Set("serverStartTime", time.Now())
|
c.Set("serverStartTime", startTime)
|
||||||
c.Next()
|
c.Next()
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -74,7 +75,6 @@ func NewRouter(
|
|||||||
// registerSystemRoutes 注册系统级路由
|
// registerSystemRoutes 注册系统级路由
|
||||||
func registerSystemRoutes(router *gin.Engine, serverHandler *handler.Server) {
|
func registerSystemRoutes(router *gin.Engine, serverHandler *handler.Server) {
|
||||||
router.GET("/health", serverHandler.Health)
|
router.GET("/health", serverHandler.Health)
|
||||||
router.GET("/stats", serverHandler.Stats)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// registerAPIRoutes 注册API路由
|
// registerAPIRoutes 注册API路由
|
||||||
|
Reference in New Issue
Block a user