mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-05 13:10:07 +00:00
refactor: simplify server & service manipulation (#993)
* refactor: simplify server & service manipulation * update * fix * update for nat, ddns & notification * chore * update cron * update dependencies * use of function iterators * update default dns servers
This commit is contained in:
@@ -3,71 +3,101 @@ package singleton
|
||||
import (
|
||||
"cmp"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"github.com/nezhahq/nezha/model"
|
||||
"github.com/nezhahq/nezha/pkg/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
ServerList map[uint64]*model.Server // [ServerID] -> model.Server
|
||||
ServerUUIDToID map[string]uint64 // [ServerUUID] -> ServerID
|
||||
ServerLock sync.RWMutex
|
||||
type ServerClass struct {
|
||||
class[uint64, *model.Server]
|
||||
|
||||
SortedServerList []*model.Server // 用于存储服务器列表的 slice,按照服务器 ID 排序
|
||||
SortedServerListForGuest []*model.Server
|
||||
SortedServerLock sync.RWMutex
|
||||
)
|
||||
uuidToID map[string]uint64
|
||||
|
||||
func InitServer() {
|
||||
ServerList = make(map[uint64]*model.Server)
|
||||
ServerUUIDToID = make(map[string]uint64)
|
||||
sortedListForGuest []*model.Server
|
||||
}
|
||||
|
||||
// loadServers 加载服务器列表并根据ID排序
|
||||
func loadServers() {
|
||||
InitServer()
|
||||
func NewServerClass() *ServerClass {
|
||||
sc := &ServerClass{
|
||||
class: class[uint64, *model.Server]{
|
||||
list: make(map[uint64]*model.Server),
|
||||
},
|
||||
uuidToID: make(map[string]uint64),
|
||||
}
|
||||
|
||||
var servers []model.Server
|
||||
DB.Find(&servers)
|
||||
for _, s := range servers {
|
||||
innerS := s
|
||||
model.InitServer(&innerS)
|
||||
ServerList[innerS.ID] = &innerS
|
||||
ServerUUIDToID[innerS.UUID] = innerS.ID
|
||||
sc.list[innerS.ID] = &innerS
|
||||
sc.uuidToID[innerS.UUID] = innerS.ID
|
||||
}
|
||||
ReSortServer()
|
||||
sc.sortList()
|
||||
|
||||
return sc
|
||||
}
|
||||
|
||||
// ReSortServer 根据服务器ID 对服务器列表进行排序(ID越大越靠前)
|
||||
func ReSortServer() {
|
||||
ServerLock.RLock()
|
||||
defer ServerLock.RUnlock()
|
||||
SortedServerLock.Lock()
|
||||
defer SortedServerLock.Unlock()
|
||||
func (c *ServerClass) Update(s *model.Server, uuid string) {
|
||||
c.listMu.Lock()
|
||||
|
||||
SortedServerList = utils.MapValuesToSlice(ServerList)
|
||||
c.list[s.ID] = s
|
||||
if uuid != "" {
|
||||
c.uuidToID[uuid] = s.ID
|
||||
}
|
||||
|
||||
c.listMu.Unlock()
|
||||
|
||||
c.sortList()
|
||||
}
|
||||
|
||||
func (c *ServerClass) Delete(idList []uint64) {
|
||||
c.listMu.Lock()
|
||||
|
||||
for _, id := range idList {
|
||||
serverUUID := c.list[id].UUID
|
||||
delete(c.uuidToID, serverUUID)
|
||||
delete(c.list, id)
|
||||
}
|
||||
|
||||
c.listMu.Unlock()
|
||||
|
||||
c.sortList()
|
||||
}
|
||||
|
||||
func (c *ServerClass) GetSortedListForGuest() []*model.Server {
|
||||
c.sortedListMu.RLock()
|
||||
defer c.sortedListMu.RUnlock()
|
||||
|
||||
return slices.Clone(c.sortedListForGuest)
|
||||
}
|
||||
|
||||
func (c *ServerClass) UUIDToID(uuid string) (id uint64, ok bool) {
|
||||
c.listMu.RLock()
|
||||
defer c.listMu.RUnlock()
|
||||
|
||||
id, ok = c.uuidToID[uuid]
|
||||
return
|
||||
}
|
||||
|
||||
func (c *ServerClass) sortList() {
|
||||
c.listMu.RLock()
|
||||
defer c.listMu.RUnlock()
|
||||
c.sortedListMu.Lock()
|
||||
defer c.sortedListMu.Unlock()
|
||||
|
||||
c.sortedList = utils.MapValuesToSlice(c.list)
|
||||
// 按照服务器 ID 排序的具体实现(ID越大越靠前)
|
||||
slices.SortStableFunc(SortedServerList, func(a, b *model.Server) int {
|
||||
slices.SortStableFunc(c.sortedList, func(a, b *model.Server) int {
|
||||
if a.DisplayIndex == b.DisplayIndex {
|
||||
return cmp.Compare(a.ID, b.ID)
|
||||
}
|
||||
return cmp.Compare(b.DisplayIndex, a.DisplayIndex)
|
||||
})
|
||||
|
||||
SortedServerListForGuest = make([]*model.Server, 0, len(SortedServerList))
|
||||
for _, s := range SortedServerList {
|
||||
c.sortedListForGuest = make([]*model.Server, 0, len(c.sortedList))
|
||||
for _, s := range c.sortedList {
|
||||
if !s.HideForGuest {
|
||||
SortedServerListForGuest = append(SortedServerListForGuest, s)
|
||||
c.sortedListForGuest = append(c.sortedListForGuest, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func OnServerDelete(sid []uint64) {
|
||||
ServerLock.Lock()
|
||||
defer ServerLock.Unlock()
|
||||
for _, id := range sid {
|
||||
serverUUID := ServerList[id].UUID
|
||||
delete(ServerUUIDToID, serverUUID)
|
||||
delete(ServerList, id)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user