init: EZOps AIOps 智能运维平台

- 前端: React 18 + TypeScript + Vite + Tailwind CSS + shadcn/ui
- 后端: Go + Gin + GORM + JWT (golang-jwt/jwt/v5)
- 功能: 服务器管理、K8s集群、监控告警、CI/CD、自动化运维、审批流程、审计日志
- 文档: 设计文档(ROADMAP/PDD/架构/模块)
This commit is contained in:
attacker committed 2026-06-13 23:09:47 +08:00
commit 7d6674bddd
156 files changed
+45704

No files matched your search

+428
View File
@@ -0,0 +1,428 @@
package handlers
import (
"encoding/json"
"net/http"
"strconv"
"ezops/internal/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// NavigationHandler 导航处理器
type NavigationHandler struct {
db *gorm.DB
}
// NewNavigationHandler 创建导航处理器
func NewNavigationHandler(db *gorm.DB) *NavigationHandler {
return &NavigationHandler{db: db}
}
// ListGroups 获取导航分组列表
// @Summary 获取导航分组列表
// @Description 获取所有导航分组列表
// @Tags 站内导航
// @Produce json
// @Security Bearer
// @Success 200 {object} models.Response{data=[]models.NavigationGroup}
// @Router /navigations/groups [get]
func (h *NavigationHandler) ListGroups(c *gin.Context) {
var groups []models.NavigationGroup
h.db.Order("sort ASC, created_at ASC").Find(&groups)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "success",
Data: groups,
})
}
// CreateGroup 创建导航分组
// @Summary 创建导航分组
// @Description 创建新导航分组
// @Tags 站内导航
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body models.CreateNavigationGroupRequest true "创建请求"
// @Success 200 {object} models.Response{data=models.NavigationGroup}
// @Router /navigations/groups [post]
func (h *NavigationHandler) CreateGroup(c *gin.Context) {
var req models.CreateNavigationGroupRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{
Code: 400,
Message: "请求参数错误: " + err.Error(),
})
return
}
group := models.NavigationGroup{
Name: req.Name,
Code: req.Code,
Description: req.Description,
Icon: req.Icon,
Sort: req.Sort,
Enabled: true,
}
if err := h.db.Create(&group).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "创建失败",
})
return
}
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "创建成功",
Data: group,
})
}
// UpdateGroup 更新导航分组
// @Summary 更新导航分组
// @Description 更新指定导航分组
// @Tags 站内导航
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "分组ID"
// @Param request body models.UpdateNavigationGroupRequest true "更新请求"
// @Success 200 {object} models.Response{data=models.NavigationGroup}
// @Router /navigations/groups/{id} [put]
func (h *NavigationHandler) UpdateGroup(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var req models.UpdateNavigationGroupRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{
Code: 400,
Message: "请求参数错误: " + err.Error(),
})
return
}
var group models.NavigationGroup
if err := h.db.First(&group, id).Error; err != nil {
c.JSON(http.StatusNotFound, models.Response{
Code: 404,
Message: "分组不存在",
})
return
}
updates := map[string]interface{}{}
if req.Name != "" {
updates["name"] = req.Name
}
if req.Description != "" {
updates["description"] = req.Description
}
if req.Icon != "" {
updates["icon"] = req.Icon
}
updates["sort"] = req.Sort
if req.Enabled != nil {
updates["enabled"] = *req.Enabled
}
if err := h.db.Model(&group).Updates(updates).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "更新失败",
})
return
}
h.db.First(&group, id)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "更新成功",
Data: group,
})
}
// DeleteGroup 删除导航分组
// @Summary 删除导航分组
// @Description 删除指定导航分组
// @Tags 站内导航
// @Produce json
// @Security Bearer
// @Param id path int true "分组ID"
// @Success 200 {object} models.Response
// @Router /navigations/groups/{id} [delete]
func (h *NavigationHandler) DeleteGroup(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
// 删除分组下的所有链接
if err := h.db.Where("group_id = ?", id).Delete(&models.NavigationLink{}).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "删除链接失败",
})
return
}
if err := h.db.Delete(&models.NavigationGroup{}, id).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "删除失败",
})
return
}
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "删除成功",
})
}
// ListLinks 获取导航链接列表
// @Summary 获取导航链接列表
// @Description 获取所有导航链接列表
// @Tags 站内导航
// @Produce json
// @Security Bearer
// @Param group_id query int false "分组ID"
// @Param keyword query string false "搜索关键词"
// @Success 200 {object} models.Response{data=[]models.NavigationLink}
// @Router /navigations/links [get]
func (h *NavigationHandler) ListLinks(c *gin.Context) {
db := h.db.Model(&models.NavigationLink{}).Preload("Group")
// 分组过滤
if groupID := c.Query("group_id"); groupID != "" {
db = db.Where("group_id = ?", groupID)
}
// 关键词搜索
if keyword := c.Query("keyword"); keyword != "" {
db = db.Where("name LIKE ? OR description LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
}
var links []models.NavigationLink
db.Order("sort ASC, created_at ASC").Find(&links)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "success",
Data: links,
})
}
// CreateLink 创建导航链接
// @Summary 创建导航链接
// @Description 创建新导航链接
// @Tags 站内导航
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body models.CreateNavigationLinkRequest true "创建请求"
// @Success 200 {object} models.Response{data=models.NavigationLink}
// @Router /navigations/links [post]
func (h *NavigationHandler) CreateLink(c *gin.Context) {
var req models.CreateNavigationLinkRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{
Code: 400,
Message: "请求参数错误: " + err.Error(),
})
return
}
tags, _ := json.Marshal(req.Tags)
link := models.NavigationLink{
GroupID: req.GroupID,
Name: req.Name,
URL: req.URL,
Description: req.Description,
Icon: req.Icon,
Tags: string(tags),
Sort: req.Sort,
Enabled: true,
}
if err := h.db.Create(&link).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "创建失败",
})
return
}
// 更新分组链接数
h.db.Model(&models.NavigationGroup{}).Where("id = ?", req.GroupID).
UpdateColumn("link_count", h.db.Model(&models.NavigationLink{}).Where("group_id = ?", req.GroupID).Select("COUNT(*)"))
h.db.Preload("Group").First(&link, link.ID)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "创建成功",
Data: link,
})
}
// UpdateLink 更新导航链接
// @Summary 更新导航链接
// @Description 更新指定导航链接
// @Tags 站内导航
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "链接ID"
// @Param request body models.UpdateNavigationLinkRequest true "更新请求"
// @Success 200 {object} models.Response{data=models.NavigationLink}
// @Router /navigations/links/{id} [put]
func (h *NavigationHandler) UpdateLink(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var req models.UpdateNavigationLinkRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{
Code: 400,
Message: "请求参数错误: " + err.Error(),
})
return
}
var link models.NavigationLink
if err := h.db.First(&link, id).Error; err != nil {
c.JSON(http.StatusNotFound, models.Response{
Code: 404,
Message: "链接不存在",
})
return
}
updates := map[string]interface{}{}
if req.GroupID > 0 {
updates["group_id"] = req.GroupID
}
if req.Name != "" {
updates["name"] = req.Name
}
if req.URL != "" {
updates["url"] = req.URL
}
if req.Description != "" {
updates["description"] = req.Description
}
if req.Icon != "" {
updates["icon"] = req.Icon
}
if len(req.Tags) > 0 {
tags, _ := json.Marshal(req.Tags)
updates["tags"] = string(tags)
}
updates["sort"] = req.Sort
if req.Enabled != nil {
updates["enabled"] = *req.Enabled
}
if err := h.db.Model(&link).Updates(updates).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "更新失败",
})
return
}
h.db.Preload("Group").First(&link, id)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "更新成功",
Data: link,
})
}
// DeleteLink 删除导航链接
// @Summary 删除导航链接
// @Description 删除指定导航链接
// @Tags 站内导航
// @Produce json
// @Security Bearer
// @Param id path int true "链接ID"
// @Success 200 {object} models.Response
// @Router /navigations/links/{id} [delete]
func (h *NavigationHandler) DeleteLink(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var link models.NavigationLink
h.db.First(&link, id)
if err := h.db.Delete(&models.NavigationLink{}, id).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "删除失败",
})
return
}
// 更新分组链接数
if link.GroupID > 0 {
h.db.Model(&models.NavigationGroup{}).Where("id = ?", link.GroupID).
UpdateColumn("link_count", h.db.Model(&models.NavigationLink{}).Where("group_id = ?", link.GroupID).Select("COUNT(*)"))
}
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "删除成功",
})
}
// ToggleFavorite 切换收藏状态
// @Summary 切换收藏状态
// @Description 切换导航链接的收藏状态
// @Tags 站内导航
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "链接ID"
// @Param request body models.ToggleFavoriteRequest true "收藏请求"
// @Success 200 {object} models.Response{data=models.NavigationLink}
// @Router /navigations/links/{id}/favorite [post]
func (h *NavigationHandler) ToggleFavorite(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var req models.ToggleFavoriteRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{
Code: 400,
Message: "请求参数错误",
})
return
}
var link models.NavigationLink
if err := h.db.First(&link, id).Error; err != nil {
c.JSON(http.StatusNotFound, models.Response{
Code: 404,
Message: "链接不存在",
})
return
}
link.IsFavorite = req.IsFavorite
if err := h.db.Save(&link).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "操作失败",
})
return
}
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "操作成功",
Data: link,
})
}