F1 告警静默规则 [done]: - 后端 alert_silence.go: CRUD + Toggle (onetime/daily/weekly 三模式) - 告警引擎 isSilenced 集成 (alert_engine.go) - 前端 SilenceRuleTab + SilenceDialog - 路由 /alerts/silences 已注册 F2 审计日志导出 [done]: - 重写 ExportAuditLogs: 真正的 CSV 导出 (encoding/csv + UTF-8 BOM) - 支持 module/action/username/startTime/endTime 筛选 - 导出限制 10000 条防溢出,自动记录审计日志 - JSON 格式向后兼容 (format=json) - 前端 exportLogs 传递查询参数 同时包含累积的前期工作: - 目录重构 internal/ -> controller/model/router (v0.55-v0.57) - 后端分层架构 dto/repository/service/utils - 审计模块全功能 (终端审计/操作审计/登录日志) - asciicast v2 录制格式 - SFTP 文件浏览器 - StatStrip 紧凑统计条 - 在线会话自动刷新 - LiveMonitor WebSocket 诊断修复 验证: go build ✅ | npm run build ✅ | Docker 部署 ✅ | 10 回归端点 ✅ | CSV 导出 ✅ | 静默 CRUD ✅
165 lines
6.1 KiB
Go
165 lines
6.1 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"ezops/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// DeployHandler 发布系统处理器
|
|
type DeployHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewDeployHandler(db *gorm.DB) *DeployHandler {
|
|
return &DeployHandler{db: db}
|
|
}
|
|
|
|
// ==================== Git 仓库认证 ====================
|
|
|
|
func (h *DeployHandler) ListRepos(c *gin.Context) {
|
|
var items []model.PipelineRepo
|
|
h.db.Order("updated_at DESC").Find(&items)
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: model.ListResponse{List: items, Total: int64(len(items))}})
|
|
}
|
|
|
|
func (h *DeployHandler) GetRepo(c *gin.Context) {
|
|
var item model.PipelineRepo
|
|
if err := h.db.First(&item, c.Param("id")); err.Error != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{Code: 404, Message: "不存在"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) CreateRepo(c *gin.Context) {
|
|
var req struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Description string `json:"description"`
|
|
URL string `json:"url" binding:"required"`
|
|
AuthType model.RepoAuthType `json:"auth_type"`
|
|
CredentialID *uint `json:"credential_id"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: err.Error()})
|
|
return
|
|
}
|
|
if req.AuthType == "" { req.AuthType = model.RepoAuthToken }
|
|
item := model.PipelineRepo{
|
|
Name: req.Name, Description: req.Description, URL: req.URL,
|
|
AuthType: req.AuthType, CredentialID: req.CredentialID,
|
|
CreatedBy: c.GetString("username"),
|
|
}
|
|
h.db.Create(&item)
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) UpdateRepo(c *gin.Context) {
|
|
var item model.PipelineRepo
|
|
if err := h.db.First(&item, c.Param("id")); err.Error != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{Code: 404, Message: "不存在"})
|
|
return
|
|
}
|
|
var req struct {
|
|
Name *string `json:"name"`
|
|
Description *string `json:"description"`
|
|
URL *string `json:"url"`
|
|
AuthType *model.RepoAuthType `json:"auth_type"`
|
|
CredentialID *uint `json:"credential_id"`
|
|
Enabled *bool `json:"enabled"`
|
|
}
|
|
c.ShouldBindJSON(&req)
|
|
updates := map[string]interface{}{}
|
|
if req.Name != nil { updates["name"] = *req.Name }
|
|
if req.Description != nil { updates["description"] = *req.Description }
|
|
if req.URL != nil { updates["url"] = *req.URL }
|
|
if req.AuthType != nil { updates["auth_type"] = *req.AuthType }
|
|
if req.Enabled != nil { updates["enabled"] = *req.Enabled }
|
|
if req.CredentialID != nil { updates["credential_id"] = req.CredentialID }
|
|
h.db.Model(&item).Updates(updates)
|
|
h.db.First(&item, item.ID)
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) DeleteRepo(c *gin.Context) {
|
|
h.db.Delete(&model.PipelineRepo{}, c.Param("id"))
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "已删除"})
|
|
}
|
|
|
|
// ==================== 镜像仓库认证 ====================
|
|
|
|
func (h *DeployHandler) ListRegistries(c *gin.Context) {
|
|
var items []model.PipelineRegistry
|
|
h.db.Order("updated_at DESC").Find(&items)
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: model.ListResponse{List: items, Total: int64(len(items))}})
|
|
}
|
|
|
|
func (h *DeployHandler) GetRegistry(c *gin.Context) {
|
|
var item model.PipelineRegistry
|
|
if err := h.db.First(&item, c.Param("id")); err.Error != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{Code: 404, Message: "不存在"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) CreateRegistry(c *gin.Context) {
|
|
var req struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Description string `json:"description"`
|
|
RegistryURL string `json:"registry_url" binding:"required"`
|
|
Namespace string `json:"namespace"`
|
|
AuthType model.RegistryAuthType `json:"auth_type"`
|
|
CredentialID *uint `json:"credential_id"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: err.Error()})
|
|
return
|
|
}
|
|
if req.AuthType == "" { req.AuthType = model.RegistryAuthPassword }
|
|
item := model.PipelineRegistry{
|
|
Name: req.Name, Description: req.Description, RegistryURL: req.RegistryURL,
|
|
Namespace: req.Namespace, AuthType: req.AuthType, CredentialID: req.CredentialID,
|
|
CreatedBy: c.GetString("username"),
|
|
}
|
|
h.db.Create(&item)
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) UpdateRegistry(c *gin.Context) {
|
|
var item model.PipelineRegistry
|
|
if err := h.db.First(&item, c.Param("id")); err.Error != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{Code: 404, Message: "不存在"})
|
|
return
|
|
}
|
|
var req struct {
|
|
Name *string `json:"name"`
|
|
Description *string `json:"description"`
|
|
RegistryURL *string `json:"registry_url"`
|
|
Namespace *string `json:"namespace"`
|
|
AuthType *model.RegistryAuthType `json:"auth_type"`
|
|
CredentialID *uint `json:"credential_id"`
|
|
Enabled *bool `json:"enabled"`
|
|
}
|
|
c.ShouldBindJSON(&req)
|
|
updates := map[string]interface{}{}
|
|
if req.Name != nil { updates["name"] = *req.Name }
|
|
if req.Description != nil { updates["description"] = *req.Description }
|
|
if req.RegistryURL != nil { updates["registry_url"] = *req.RegistryURL }
|
|
if req.Namespace != nil { updates["namespace"] = *req.Namespace }
|
|
if req.AuthType != nil { updates["auth_type"] = *req.AuthType }
|
|
if req.Enabled != nil { updates["enabled"] = *req.Enabled }
|
|
if req.CredentialID != nil { updates["credential_id"] = req.CredentialID }
|
|
h.db.Model(&item).Updates(updates)
|
|
h.db.First(&item, item.ID)
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
func (h *DeployHandler) DeleteRegistry(c *gin.Context) {
|
|
h.db.Delete(&model.PipelineRegistry{}, c.Param("id"))
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "已删除"})
|
|
}
|