From 3228cdad64004fb70f02000ee7b66c13deecc0ee Mon Sep 17 00:00:00 2001 From: tbphp Date: Mon, 7 Jul 2025 15:50:47 +0800 Subject: [PATCH] feat: key status --- internal/handler/handler.go | 2 +- internal/handler/key_handler.go | 3 +-- internal/models/types.go | 6 ++++++ internal/services/key_cron_service.go | 6 +++--- 4 files changed, 11 insertions(+), 6 deletions(-) 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." } }