2024-10-20 11:23:04 -04:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-10-21 04:22:30 -04:00
|
|
|
|
2024-10-20 11:23:04 -04:00
|
|
|
"github.com/naiba/nezha/model"
|
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
|
|
|
"github.com/naiba/nezha/service/singleton"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Edit server
|
|
|
|
// @Summary Edit server
|
|
|
|
// @Security BearerAuth
|
|
|
|
// @Schemes
|
|
|
|
// @Description Edit server
|
|
|
|
// @Tags auth required
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} model.CommonResponse[any]
|
|
|
|
// @Router /server/{id} [patch]
|
2024-10-21 02:30:50 -04:00
|
|
|
func editServer(c *gin.Context) error {
|
2024-10-20 11:23:04 -04:00
|
|
|
idStr := c.Param("id")
|
|
|
|
id, err := strconv.ParseUint(idStr, 10, 64)
|
|
|
|
if err != nil {
|
2024-10-21 02:30:50 -04:00
|
|
|
return err
|
2024-10-20 11:23:04 -04:00
|
|
|
}
|
|
|
|
var sf model.EditServer
|
|
|
|
var s model.Server
|
|
|
|
if err := c.ShouldBindJSON(&sf); err != nil {
|
2024-10-21 02:30:50 -04:00
|
|
|
return err
|
2024-10-20 11:23:04 -04:00
|
|
|
}
|
|
|
|
s.Name = sf.Name
|
|
|
|
s.DisplayIndex = sf.DisplayIndex
|
|
|
|
s.ID = id
|
|
|
|
s.Note = sf.Note
|
|
|
|
s.PublicNote = sf.PublicNote
|
|
|
|
s.HideForGuest = sf.HideForGuest
|
|
|
|
s.EnableDDNS = sf.EnableDDNS
|
|
|
|
s.DDNSProfiles = sf.DDNSProfiles
|
|
|
|
ddnsProfilesRaw, err := utils.Json.Marshal(s.DDNSProfiles)
|
|
|
|
if err != nil {
|
2024-10-21 02:30:50 -04:00
|
|
|
return err
|
2024-10-20 11:23:04 -04:00
|
|
|
}
|
|
|
|
s.DDNSProfilesRaw = string(ddnsProfilesRaw)
|
|
|
|
|
|
|
|
if err := singleton.DB.Save(&s).Error; err != nil {
|
2024-10-21 02:30:50 -04:00
|
|
|
return newGormError("%v", err)
|
2024-10-20 11:23:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
singleton.ServerLock.Lock()
|
|
|
|
s.CopyFromRunningServer(singleton.ServerList[s.ID])
|
|
|
|
singleton.ServerList[s.ID] = &s
|
|
|
|
singleton.ServerLock.Unlock()
|
|
|
|
singleton.ReSortServer()
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
})
|
2024-10-21 02:30:50 -04:00
|
|
|
return nil
|
2024-10-20 11:23:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Batch delete server
|
|
|
|
// @Summary Batch delete server
|
|
|
|
// @Security BearerAuth
|
|
|
|
// @Schemes
|
|
|
|
// @Description Batch delete server
|
|
|
|
// @Tags auth required
|
|
|
|
// @Accept json
|
|
|
|
// @param request body []uint64 true "id list"
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} model.CommonResponse[any]
|
|
|
|
// @Router /batch-delete/server [post]
|
2024-10-21 02:30:50 -04:00
|
|
|
func batchDeleteServer(c *gin.Context) error {
|
2024-10-20 11:23:04 -04:00
|
|
|
var servers []uint64
|
|
|
|
if err := c.ShouldBindJSON(&servers); err != nil {
|
2024-10-21 02:30:50 -04:00
|
|
|
return err
|
2024-10-20 11:23:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := singleton.DB.Unscoped().Delete(&model.Server{}, "id in (?)", servers).Error; err != nil {
|
2024-10-21 02:30:50 -04:00
|
|
|
return newGormError("%v", err)
|
2024-10-20 11:23:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
singleton.ServerLock.Lock()
|
|
|
|
for i := 0; i < len(servers); i++ {
|
|
|
|
id := servers[i]
|
|
|
|
delete(singleton.ServerList, id)
|
|
|
|
|
|
|
|
singleton.AlertsLock.Lock()
|
|
|
|
for i := 0; i < len(singleton.Alerts); i++ {
|
|
|
|
if singleton.AlertsCycleTransferStatsStore[singleton.Alerts[i].ID] != nil {
|
|
|
|
delete(singleton.AlertsCycleTransferStatsStore[singleton.Alerts[i].ID].ServerName, id)
|
|
|
|
delete(singleton.AlertsCycleTransferStatsStore[singleton.Alerts[i].ID].Transfer, id)
|
|
|
|
delete(singleton.AlertsCycleTransferStatsStore[singleton.Alerts[i].ID].NextUpdate, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
singleton.AlertsLock.Unlock()
|
|
|
|
|
|
|
|
singleton.DB.Unscoped().Delete(&model.Transfer{}, "server_id = ?", id)
|
|
|
|
}
|
|
|
|
singleton.ServerLock.Unlock()
|
|
|
|
|
|
|
|
singleton.ReSortServer()
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, model.CommonResponse[interface{}]{
|
|
|
|
Success: true,
|
|
|
|
})
|
2024-10-21 02:30:50 -04:00
|
|
|
return nil
|
2024-10-20 11:23:04 -04:00
|
|
|
}
|