refactor: backend server.go split (446→336+121 lines, core CRUD + batch ops)
This commit is contained in:
1 parent
bc5bd96aff
commit
49dcaade4e
2 files changed
+121
-110
No files matched your search
@@ -1,10 +1,8 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"ezops/internal/models"
|
||||
|
||||
@@ -179,80 +177,6 @@ func (h *ServerHandler) CreateServer(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// BatchCreateServers 批量导入服务器 // Logan.Li
|
||||
// 接收 {servers:[...], region_id, auth_type},逐条校验并创建,跳过重复(主机名或IP已存在)
|
||||
func (h *ServerHandler) BatchCreateServers(c *gin.Context) {
|
||||
var req models.BatchCreateServerRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{
|
||||
Code: 400,
|
||||
Message: "请求参数错误: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
authType := strings.TrimSpace(req.AuthType)
|
||||
if authType == "" {
|
||||
authType = "password"
|
||||
}
|
||||
|
||||
result := models.BatchCreateResult{Errors: []string{}}
|
||||
for i, item := range req.Servers {
|
||||
hostname := strings.TrimSpace(item.Hostname)
|
||||
ip := strings.TrimSpace(item.IP)
|
||||
if hostname == "" || ip == "" {
|
||||
result.Failed++
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("第%d行: 主机名和IP不能为空", i+2))
|
||||
continue
|
||||
}
|
||||
|
||||
// 重复检查(主机名或 IP 已存在则跳过)
|
||||
var count int64
|
||||
h.db.Model(&models.Server{}).Where("hostname = ? OR ip = ?", hostname, ip).Count(&count)
|
||||
if count > 0 {
|
||||
result.Skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
port := item.Port
|
||||
if port == 0 {
|
||||
port = 22
|
||||
}
|
||||
username := strings.TrimSpace(item.Username)
|
||||
if username == "" {
|
||||
username = "root"
|
||||
}
|
||||
|
||||
server := models.Server{
|
||||
Hostname: hostname,
|
||||
IP: ip,
|
||||
Port: port,
|
||||
Username: username,
|
||||
AuthType: authType,
|
||||
OS: item.OS,
|
||||
CPU: item.CPU,
|
||||
Memory: item.Memory * 1024, // GB → MB
|
||||
Disk: item.Disk,
|
||||
Tags: item.Tags,
|
||||
RegionID: req.RegionID,
|
||||
Status: "stopped",
|
||||
}
|
||||
if err := h.db.Create(&server).Error; err != nil {
|
||||
result.Failed++
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("%s(%s): %s", hostname, ip, err.Error()))
|
||||
continue
|
||||
}
|
||||
result.Success++
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("导入完成: 成功 %d, 跳过 %d, 失败 %d", result.Success, result.Skipped, result.Failed)
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: msg,
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateServer 更新服务器
|
||||
// @Summary 更新服务器
|
||||
// @Description 更新指定服务器信息
|
||||
@@ -378,40 +302,6 @@ func (h *ServerHandler) DeleteServer(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// BatchDeleteServers 批量删除服务器
|
||||
// @Summary 批量删除服务器
|
||||
// @Description 批量删除服务器
|
||||
// @Tags 服务器
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param request body models.IDsRequest true "删除请求"
|
||||
// @Success 200 {object} models.Response
|
||||
// @Router /servers/batch-delete [post]
|
||||
func (h *ServerHandler) BatchDeleteServers(c *gin.Context) {
|
||||
var req models.IDsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{
|
||||
Code: 400,
|
||||
Message: "请求参数错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.Delete(&models.Server{}, req.IDs).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{
|
||||
Code: 500,
|
||||
Message: "删除失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "删除成功",
|
||||
})
|
||||
}
|
||||
|
||||
// GetServerStats 获取服务器统计
|
||||
// @Summary 获取服务器统计
|
||||
// @Description 获取服务器统计数据
|
||||
|
||||
Reference in new issue
Block a user