feat(api): add reset sort endpoints for server and node

This commit is contained in:
Chang lue Tsen
2025-09-01 11:41:34 -04:00
parent e910d0e345
commit 367ef9d2e7
12 changed files with 288 additions and 89 deletions
+7 -7
View File
@@ -12,8 +12,8 @@ import (
type (
customCacheLogicModel interface {
StatusCache(ctx context.Context, serverId int64, protocol string) (Status, error)
UpdateStatusCache(ctx context.Context, serverId int64, protocol string, status *Status) error
StatusCache(ctx context.Context, serverId int64) (Status, error)
UpdateStatusCache(ctx context.Context, serverId int64, status *Status) error
OnlineUserSubscribe(ctx context.Context, serverId int64, protocol string) (OnlineUserSubscribe, error)
UpdateOnlineUserSubscribe(ctx context.Context, serverId int64, protocol string, subscribe OnlineUserSubscribe) error
OnlineUserSubscribeGlobal(ctx context.Context) (int64, error)
@@ -54,22 +54,22 @@ func (s *Status) Unmarshal(data string) error {
const (
Expiry = 300 * time.Second // Cache expiry time in seconds
StatusCacheKey = "node:status:%d:%s" // Node status cache key format (Server ID and protocol) Example: node:status:1:shadowsocks
StatusCacheKey = "node:status:%d" // Node status cache key format (Server ID and protocol) Example: node:status:1:shadowsocks
OnlineUserCacheKeyWithSubscribe = "node:online:subscribe:%d:%s" // Online user subscribe cache key format (Server ID and protocol) Example: node:online:subscribe:1:shadowsocks
OnlineUserSubscribeCacheKeyWithGlobal = "node:online:subscribe:global" // Online user global subscribe cache key
)
// UpdateStatusCache Update server status to cache
func (m *customServerModel) UpdateStatusCache(ctx context.Context, serverId int64, protocol string, status *Status) error {
key := fmt.Sprintf(StatusCacheKey, serverId, protocol)
func (m *customServerModel) UpdateStatusCache(ctx context.Context, serverId int64, status *Status) error {
key := fmt.Sprintf(StatusCacheKey, serverId)
return m.Cache.Set(ctx, key, status.Marshal(), Expiry).Err()
}
// StatusCache Get server status from cache
func (m *customServerModel) StatusCache(ctx context.Context, serverId int64, protocol string) (Status, error) {
func (m *customServerModel) StatusCache(ctx context.Context, serverId int64) (Status, error) {
var status Status
key := fmt.Sprintf(StatusCacheKey, serverId, protocol)
key := fmt.Sprintf(StatusCacheKey, serverId)
result, err := m.Cache.Get(ctx, key).Result()
if err != nil {
+18 -2
View File
@@ -1,6 +1,10 @@
package node
import "time"
import (
"time"
"gorm.io/gorm"
)
type Node struct {
Id int64 `gorm:"primary_key"`
@@ -12,10 +16,22 @@ type Node struct {
Server *Server `gorm:"foreignKey:ServerId;references:Id"`
Protocol string `gorm:"type:varchar(100);not null;default:'';comment:Protocol"`
Enabled *bool `gorm:"type:boolean;not null;default:true;comment:Enabled"`
Sort int `gorm:"uniqueIndex;not null;default:0;comment:Sort"`
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
}
func (Node) TableName() string {
func (n *Node) TableName() string {
return "nodes"
}
func (n *Node) BeforeCreate(tx *gorm.DB) error {
if n.Sort == 0 {
var maxSort int
if err := tx.Model(&Node{}).Select("COALESCE(MAX(sort), 0)").Scan(&maxSort).Error; err != nil {
return err
}
n.Sort = maxSort + 1
}
return nil
}
+12
View File
@@ -5,6 +5,7 @@ import (
"time"
"github.com/pkg/errors"
"gorm.io/gorm"
)
type Server struct {
@@ -25,6 +26,17 @@ func (*Server) TableName() string {
return "servers"
}
func (m *Server) BeforeCreate(tx *gorm.DB) error {
if m.Sort == 0 {
var maxSort int
if err := tx.Model(&Server{}).Select("COALESCE(MAX(sort), 0)").Scan(&maxSort).Error; err != nil {
return err
}
m.Sort = maxSort + 1
}
return nil
}
// MarshalProtocols Marshal server protocols to json
func (m *Server) MarshalProtocols(list []Protocol) error {
var validate = make(map[string]bool)