新增API管理页面

This commit is contained in:
2025-11-11 16:05:58 +08:00
parent fdf0c1442c
commit cdeb4ea9fb
7 changed files with 530 additions and 0 deletions

View File

@@ -555,3 +555,120 @@ func (h *APIHandler) ForceLogCleanupHandler(c *gin.Context) {
"success": report.Success,
})
}
// ============ API Key 管理相关处理器 ============
// APIKeyListResponse API Key列表响应
type APIKeyListResponse struct {
ID uint `json:"id"`
Key string `json:"key"`
CreatedAt time.Time `json:"created_at"`
}
// GetAPIKeysHandler 获取所有API Key列表
func (h *APIHandler) GetAPIKeysHandler(c *gin.Context) {
var apiKeys []models.APIKey
// 查询所有API Key
if err := h.DB.Order("created_at DESC").Find(&apiKeys).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch API keys"})
return
}
// 转换为响应格式
response := make([]APIKeyListResponse, len(apiKeys))
for i, key := range apiKeys {
response[i] = APIKeyListResponse{
ID: key.ID,
Key: key.Key,
CreatedAt: key.CreatedAt,
}
}
c.JSON(http.StatusOK, gin.H{
"api_keys": response,
"total": len(response),
})
}
// CreateAPIKeyRequest 创建API Key的请求结构
type CreateAPIKeyRequest struct {
Key string `json:"key" binding:"required"`
}
// CreateAPIKeyHandler 创建新的API Key
func (h *APIHandler) CreateAPIKeyHandler(c *gin.Context) {
var req CreateAPIKeyRequest
// 解析请求
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format: " + err.Error()})
return
}
// 验证Key不为空
if req.Key == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "API key cannot be empty"})
return
}
// 检查Key是否已存在
var existingKey models.APIKey
if err := h.DB.Where("key = ?", req.Key).First(&existingKey).Error; err == nil {
c.JSON(http.StatusConflict, gin.H{"error": "API key already exists"})
return
}
// 创建新的API Key
newAPIKey := models.APIKey{
Key: req.Key,
}
if err := h.DB.Create(&newAPIKey).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create API key"})
return
}
// 返回创建的API Key
c.JSON(http.StatusCreated, gin.H{
"message": "API key created successfully",
"api_key": APIKeyListResponse{
ID: newAPIKey.ID,
Key: newAPIKey.Key,
CreatedAt: newAPIKey.CreatedAt,
},
})
}
// DeleteAPIKeyHandler 删除指定的API Key
func (h *APIHandler) DeleteAPIKeyHandler(c *gin.Context) {
// 获取API Key ID
keyID := c.Param("id")
if keyID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "API key ID is required"})
return
}
// 查找API Key
var apiKey models.APIKey
if err := h.DB.First(&apiKey, keyID).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "API key not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to find API key"})
return
}
// 删除API Key
if err := h.DB.Delete(&apiKey).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete API key"})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "API key deleted successfully",
"id": apiKey.ID,
})
}

View File

@@ -118,6 +118,11 @@ func main() {
// Log Cleaner Management
api_.GET("/log-cleaner/status", handler.GetLogCleanerStatusHandler)
api_.POST("/log-cleaner/force-cleanup", handler.ForceLogCleanupHandler)
// API Keys Management
api_.GET("/api-keys", handler.GetAPIKeysHandler)
api_.POST("/api-keys", handler.CreateAPIKeyHandler)
api_.DELETE("/api-keys/:id", handler.DeleteAPIKeyHandler)
}
// 设置优雅关闭信号处理