diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 82d5411..a426b5d 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -103,7 +103,7 @@ func (s *Server) Login(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 = ?", "active").Count(&healthyKeys) + s.DB.Model(&models.APIKey{}).Where("status = ?", models.KeyStatusActive).Count(&healthyKeys) status := "healthy" httpStatus := http.StatusOK diff --git a/internal/handler/key_handler.go b/internal/handler/key_handler.go index 1668d99..75c0a24 100644 --- a/internal/handler/key_handler.go +++ b/internal/handler/key_handler.go @@ -108,7 +108,7 @@ func (s *Server) ListKeysInGroup(c *gin.Context) { } statusFilter := c.Query("status") - if statusFilter != "" && statusFilter != "active" && statusFilter != "inactive" { + if statusFilter != "" && statusFilter != models.KeyStatusActive && statusFilter != models.KeyStatusInvalid { response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "Invalid status filter")) return } @@ -254,4 +254,3 @@ func (s *Server) ClearAllInvalidKeys(c *gin.Context) { response.Success(c, gin.H{"message": fmt.Sprintf("%d invalid keys cleared.", rowsAffected)}) } - diff --git a/internal/models/types.go b/internal/models/types.go index 05d71c2..9cfea91 100644 --- a/internal/models/types.go +++ b/internal/models/types.go @@ -6,6 +6,12 @@ import ( "gorm.io/datatypes" ) +// Key状态 +const ( + KeyStatusActive = "active" + KeyStatusInvalid = "invalid" +) + // SystemSetting 对应 system_settings 表 type SystemSetting struct { ID uint `gorm:"primaryKey;autoIncrement" json:"id"` diff --git a/internal/services/key_cron_service.go b/internal/services/key_cron_service.go index 9ea2f4f..50e8c40 100644 --- a/internal/services/key_cron_service.go +++ b/internal/services/key_cron_service.go @@ -172,14 +172,14 @@ func (s *KeyCronService) processResults() { var newErrorReason string if result.Error != nil { - newStatus = "inactive" + newStatus = models.KeyStatusInvalid newErrorReason = result.Error.Error() } else { if result.IsValid { - newStatus = "active" + newStatus = models.KeyStatusActive newErrorReason = "" } else { - newStatus = "inactive" + newStatus = models.KeyStatusInvalid newErrorReason = "Validation returned false without a specific error." } }