refactor: 全局代码模块化重构 — 所有文件<700行
后端 handler 拆分: - automation.go(1067→4文件), server.go(1011→5文件), ldap.go(815→3文件), user.go(704→2文件) 前端组件拆分: - SystemManager(1782→569), AlertManager(1133→220), K8sClusters(1062→4文件) - Dashboard(941→4文件), ServerManager(905→640), ApprovalFlow(830→548) - AlertCenter(775→693), AIPredictiveAnalytics(760→670) - CloudBilling(728→663), GrafanaDashboard(717→643), sidebar(726→689) Mock 数据模块化: - mocks/data.ts(2552行→barrel+11个domain模块) 新增 CLAUDE.md 项目全局开发规范 修复: AlertCenter/LogManager ListResponse.data→.list 类型不匹配 验证: go build ✅ + tsc --noEmit ✅ + npm run build ✅
This commit is contained in:
1 parent
e8dbe65955
commit
73b5a48df9
37 files changed
+5148
-5237
No files matched your search
@@ -0,0 +1,249 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"ezops/internal/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// AnsibleHandler Ansible 自动化处理器 // Logan.Li
|
||||
type AnsibleHandler struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewAnsibleHandler(db *gorm.DB) *AnsibleHandler {
|
||||
return &AnsibleHandler{db: db}
|
||||
}
|
||||
|
||||
// ==================== Playbook ====================
|
||||
|
||||
func (h *AnsibleHandler) ListPlaybooks(c *gin.Context) {
|
||||
var query models.PageQuery
|
||||
c.ShouldBindQuery(&query)
|
||||
|
||||
db := h.db.Model(&models.AnsiblePlaybook{})
|
||||
if name := c.Query("name"); name != "" {
|
||||
db = db.Where("name LIKE ?", "%"+name+"%")
|
||||
}
|
||||
if cat := c.Query("category"); cat != "" {
|
||||
db = db.Where("category = ?", cat)
|
||||
}
|
||||
|
||||
var total int64
|
||||
db.Count(&total)
|
||||
|
||||
var items []models.AnsiblePlaybook
|
||||
db.Order("updated_at DESC").Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&items)
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: models.ListResponse{List: items, Total: total}})
|
||||
}
|
||||
|
||||
func (h *AnsibleHandler) GetPlaybook(c *gin.Context) {
|
||||
var item models.AnsiblePlaybook
|
||||
if err := h.db.First(&item, c.Param("id")); err.Error != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "Playbook 不存在"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
||||
}
|
||||
|
||||
func (h *AnsibleHandler) CreatePlaybook(c *gin.Context) {
|
||||
var req struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
Content string `json:"content" binding:"required"`
|
||||
Tags string `json:"tags"`
|
||||
Category string `json:"category"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
username := c.GetString("username")
|
||||
item := models.AnsiblePlaybook{
|
||||
Name: req.Name, Description: req.Description, Content: req.Content,
|
||||
Tags: req.Tags, Category: req.Category, CreatedBy: username,
|
||||
}
|
||||
if err := h.db.Create(&item); err.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "创建失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
||||
}
|
||||
|
||||
func (h *AnsibleHandler) UpdatePlaybook(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
var item models.AnsiblePlaybook
|
||||
if err := h.db.First(&item, id); err.Error != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Content *string `json:"content"`
|
||||
Tags *string `json:"tags"`
|
||||
Category *string `json:"category"`
|
||||
}
|
||||
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.Content != nil { updates["content"] = *req.Content }
|
||||
if req.Tags != nil { updates["tags"] = *req.Tags }
|
||||
if req.Category != nil { updates["category"] = *req.Category }
|
||||
|
||||
h.db.Model(&item).Updates(updates)
|
||||
h.db.First(&item, id)
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
||||
}
|
||||
|
||||
func (h *AnsibleHandler) DeletePlaybook(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.db.Delete(&models.AnsiblePlaybook{}, id); err.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "删除失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "已删除"})
|
||||
}
|
||||
|
||||
// ==================== Job ====================
|
||||
|
||||
func (h *AnsibleHandler) ListJobs(c *gin.Context) {
|
||||
var query models.PageQuery
|
||||
c.ShouldBindQuery(&query)
|
||||
|
||||
db := h.db.Model(&models.AnsibleJob{})
|
||||
if status := c.Query("status"); status != "" {
|
||||
db = db.Where("status = ?", status)
|
||||
}
|
||||
if pbID := c.Query("playbook_id"); pbID != "" {
|
||||
db = db.Where("playbook_id = ?", pbID)
|
||||
}
|
||||
|
||||
var total int64
|
||||
db.Count(&total)
|
||||
|
||||
var items []models.AnsibleJob
|
||||
db.Order("created_at DESC").Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&items)
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: models.ListResponse{List: items, Total: total}})
|
||||
}
|
||||
|
||||
func (h *AnsibleHandler) GetJob(c *gin.Context) {
|
||||
var item models.AnsibleJob
|
||||
if err := h.db.First(&item, c.Param("id")); err.Error != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "不存在"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
||||
}
|
||||
|
||||
// ExecutePlaybook 执行 Playbook(模拟执行) // Logan.Li
|
||||
func (h *AnsibleHandler) ExecutePlaybook(c *gin.Context) {
|
||||
pbID := c.Param("id")
|
||||
var playbook models.AnsiblePlaybook
|
||||
if err := h.db.First(&playbook, pbID); err.Error != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "Playbook 不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
TargetHosts []string `json:"target_hosts"`
|
||||
Variables map[string]interface{} `json:"variables"`
|
||||
}
|
||||
c.ShouldBindJSON(&req)
|
||||
|
||||
username := c.GetString("username")
|
||||
hostsJSON, _ := json.Marshal(req.TargetHosts)
|
||||
varsJSON, _ := json.Marshal(req.Variables)
|
||||
|
||||
now := time.Now()
|
||||
job := models.AnsibleJob{
|
||||
PlaybookID: playbook.ID,
|
||||
PlaybookName: playbook.Name,
|
||||
Status: "success",
|
||||
TargetHosts: string(hostsJSON),
|
||||
Variables: string(varsJSON),
|
||||
Output: "PLAY [" + playbook.Name + "] ***\n\nTASK [执行完成] ***\nok: " +
|
||||
func() string { if len(req.TargetHosts) > 0 { return req.TargetHosts[0] }; return "localhost" }() + "\n\nPLAY RECAP ***\nok=1 changed=0 unreachable=0 failed=0",
|
||||
Duration: 1500,
|
||||
TriggeredBy: username,
|
||||
StartedAt: &now,
|
||||
CompletedAt: &now,
|
||||
}
|
||||
|
||||
if err := h.db.Create(&job); err.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "创建执行记录失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: job})
|
||||
}
|
||||
|
||||
// ==================== Inventory ====================
|
||||
|
||||
func (h *AnsibleHandler) ListInventories(c *gin.Context) {
|
||||
var items []models.AnsibleInventory
|
||||
h.db.Order("updated_at DESC").Find(&items)
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: models.ListResponse{List: items, Total: int64(len(items))}})
|
||||
}
|
||||
|
||||
func (h *AnsibleHandler) CreateInventory(c *gin.Context) {
|
||||
var req struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
Hosts interface{} `json:"hosts"` // 数组
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
hostsJSON, _ := json.Marshal(req.Hosts)
|
||||
item := models.AnsibleInventory{
|
||||
Name: req.Name, Description: req.Description,
|
||||
Hosts: string(hostsJSON), CreatedBy: c.GetString("username"),
|
||||
}
|
||||
h.db.Create(&item)
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
||||
}
|
||||
|
||||
func (h *AnsibleHandler) UpdateInventory(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
var item models.AnsibleInventory
|
||||
if err := h.db.First(&item, id); err.Error != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "不存在"})
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Hosts interface{} `json:"hosts"`
|
||||
}
|
||||
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.Hosts != nil {
|
||||
hostsJSON, _ := json.Marshal(req.Hosts)
|
||||
updates["hosts"] = string(hostsJSON)
|
||||
}
|
||||
h.db.Model(&item).Updates(updates)
|
||||
h.db.First(&item, id)
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
||||
}
|
||||
|
||||
func (h *AnsibleHandler) DeleteInventory(c *gin.Context) {
|
||||
if err := h.db.Delete(&models.AnsibleInventory{}, c.Param("id")); err.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "删除失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "已删除"})
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"ezops/internal/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// LogHandler 日志监控处理器 // Logan.Li
|
||||
type LogHandler struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewLogHandler(db *gorm.DB) *LogHandler {
|
||||
return &LogHandler{db: db}
|
||||
}
|
||||
|
||||
// QueryLogs 查询日志条目(支持 level/service/host/keyword 过滤) // Logan.Li
|
||||
func (h *LogHandler) QueryLogs(c *gin.Context) {
|
||||
var query models.PageQuery
|
||||
c.ShouldBindQuery(&query)
|
||||
|
||||
db := h.db.Model(&models.LogEntry{})
|
||||
|
||||
// 过滤
|
||||
if level := c.Query("level"); level != "" {
|
||||
db = db.Where("level = ?", level)
|
||||
}
|
||||
if service := c.Query("service"); service != "" {
|
||||
db = db.Where("service = ?", service)
|
||||
}
|
||||
if host := c.Query("host"); host != "" {
|
||||
db = db.Where("host = ?", host)
|
||||
}
|
||||
if kw := c.Query("keyword"); kw != "" {
|
||||
db = db.Where("message LIKE ?", "%"+kw+"%")
|
||||
}
|
||||
if sourceID := c.Query("source_id"); sourceID != "" {
|
||||
db = db.Where("source_id = ?", sourceID)
|
||||
}
|
||||
|
||||
var total int64
|
||||
db.Count(&total)
|
||||
|
||||
var logs []models.LogEntry
|
||||
db.Order("timestamp DESC").Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&logs)
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: models.ListResponse{List: logs, Total: total}})
|
||||
}
|
||||
|
||||
// ListSources 获取日志源列表 // Logan.Li
|
||||
func (h *LogHandler) ListSources(c *gin.Context) {
|
||||
var sources []models.LogSource
|
||||
h.db.Order("updated_at DESC").Find(&sources)
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: models.ListResponse{List: sources, Total: int64(len(sources))}})
|
||||
}
|
||||
|
||||
// CreateSource 创建日志源 // Logan.Li
|
||||
func (h *LogHandler) CreateSource(c *gin.Context) {
|
||||
var req struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Type string `json:"type" binding:"required"` // application/syslog/docker/kubernetes
|
||||
Config map[string]interface{} `json:"config"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
configJSON, _ := json.Marshal(req.Config)
|
||||
now := time.Now()
|
||||
item := models.LogSource{
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Status: "active",
|
||||
Config: string(configJSON),
|
||||
LastSeen: &now,
|
||||
}
|
||||
h.db.Create(&item)
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: item})
|
||||
}
|
||||
|
||||
// DeleteSource 删除日志源 // Logan.Li
|
||||
func (h *LogHandler) DeleteSource(c *gin.Context) {
|
||||
if err := h.db.Delete(&models.LogSource{}, c.Param("id")); err.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "删除失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "已删除"})
|
||||
}
|
||||
|
||||
// IngestLogs Agent 上报日志(无需 JWT,供 Agent 调用) // Logan.Li
|
||||
func (h *LogHandler) IngestLogs(c *gin.Context) {
|
||||
var req struct {
|
||||
Source string `json:"source"`
|
||||
Host string `json:"host"`
|
||||
Service string `json:"service"`
|
||||
Entries []map[string]interface{} `json:"entries"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 查找或创建日志源
|
||||
var source models.LogSource
|
||||
h.db.Where("name = ?", req.Source).FirstOrCreate(&source, models.LogSource{
|
||||
Name: req.Source, Type: "agent", Status: "active",
|
||||
})
|
||||
|
||||
count := 0
|
||||
for _, entry := range req.Entries {
|
||||
level, _ := entry["level"].(string)
|
||||
msg, _ := entry["message"].(string)
|
||||
raw, _ := entry["raw"].(string)
|
||||
traceID, _ := entry["trace_id"].(string)
|
||||
ts := time.Now()
|
||||
if t, ok := entry["timestamp"].(string); ok {
|
||||
if parsed, err := time.Parse(time.RFC3339, t); err == nil {
|
||||
ts = parsed
|
||||
}
|
||||
}
|
||||
|
||||
log := models.LogEntry{
|
||||
SourceID: source.ID,
|
||||
Timestamp: ts,
|
||||
Level: level,
|
||||
Service: req.Service,
|
||||
Host: req.Host,
|
||||
Message: msg,
|
||||
TraceID: traceID,
|
||||
Raw: raw,
|
||||
}
|
||||
if err := h.db.Create(&log); err.Error == nil {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
// 更新日志源统计
|
||||
now := time.Now()
|
||||
h.db.Model(&source).Updates(map[string]interface{}{
|
||||
"last_seen": &now,
|
||||
}).UpdateColumn("log_count", source.LogCount+int64(count))
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "成功写入", Data: map[string]int{"count": count}})
|
||||
}
|
||||
@@ -335,7 +335,17 @@ func (h *UserHandler) BatchImportUsers(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// ImportTemplate 下载用户导入模板
|
||||
// ImportTemplate 下载导入模板
|
||||
// (已有实现,在文件末尾追加 ListRoles)
|
||||
|
||||
// ListRoles 获取角色列表(供前端下拉选择) // Logan.Li
|
||||
func (h *UserHandler) ListRoles(c *gin.Context) {
|
||||
var roles []models.Role
|
||||
h.db.Where("status = ?", 1).Order("id ASC").Find(&roles)
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200, Data: models.ListResponse{List: roles, Total: int64(len(roles))},
|
||||
})
|
||||
}
|
||||
// GET /api/v1/admin/users/import-template
|
||||
func (h *UserHandler) ImportTemplate(c *gin.Context) {
|
||||
// UTF-8 BOM for Excel compatibility
|
||||
|
||||
Reference in new issue
Block a user