ecs/servers/admin/server.go

117 lines
3.6 KiB
Go
Raw Permalink Normal View History

2025-06-04 18:17:39 +08:00
package main
import (
"ecs/servers/admin/handler"
"github.com/oylshe1314/framework/client/rpc"
"github.com/oylshe1314/framework/client/sd"
"github.com/oylshe1314/framework/client/sd/zk"
"github.com/oylshe1314/framework/errors"
"github.com/oylshe1314/framework/server"
"github.com/oylshe1314/framework/util"
"time"
)
type ServerConfig struct {
2025-07-16 10:05:22 +08:00
Subscriptions []string `json:"Subscriptions"`
2025-06-04 18:17:39 +08:00
}
type adminServer struct {
2025-07-16 10:05:22 +08:00
server.NamedServer
2025-06-04 18:17:39 +08:00
2025-07-16 10:05:22 +08:00
serverConfig *ServerConfig
2025-06-04 18:17:39 +08:00
2025-07-16 10:05:22 +08:00
ExterServer server.HttpServer
2025-06-04 18:17:39 +08:00
2025-07-16 10:05:22 +08:00
sdConfig *sd.Config
HttpRpcClient rpc.HttpRpcClient
2025-06-04 18:17:39 +08:00
SubscribeClient sd.SubscribeClient
innerHandler *handler.InnerHandler
}
func NewAdminServer() server.Server {
return &adminServer{}
}
2025-07-16 10:05:22 +08:00
func (this *adminServer) WithServerConfig(serverConfig *ServerConfig) {
this.serverConfig = serverConfig
}
func (this *adminServer) WithSdConfig(config *sd.Config) {
this.sdConfig = config
}
2025-06-04 18:17:39 +08:00
2025-07-16 10:05:22 +08:00
func (this *adminServer) Init() (err error) {
err = this.NamedServer.Init()
2025-06-04 18:17:39 +08:00
if err != nil {
return err
}
2025-07-16 10:05:22 +08:00
this.ExterServer.SetServer(this)
err = this.ExterServer.Init()
2025-06-04 18:17:39 +08:00
if err != nil {
2025-07-16 10:05:22 +08:00
return errors.Errorf("the 'ExterServer' init failed, %v", err)
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
this.HttpRpcClient = rpc.NewHttpRpcClient(this.Logger())
2025-06-04 18:17:39 +08:00
err = this.HttpRpcClient.Init()
if err != nil {
return errors.Errorf("the 'HttpRpcClient' init failed, %v", err)
}
2025-07-16 10:05:22 +08:00
var subscriptions = map[string]sd.SubscribeCallback{}
for _, svrName := range this.serverConfig.Subscriptions {
subscriptions[svrName] = this.HttpRpcClient.SubscribeCallback
2025-06-04 18:17:39 +08:00
}
2025-07-16 10:05:22 +08:00
this.SubscribeClient = zk.NewSubscribeClient(this, this.sdConfig, subscriptions)
2025-06-04 18:17:39 +08:00
err = this.SubscribeClient.Init()
if err != nil {
return errors.Errorf("the 'SubscribeClient' init failed, %v", err)
}
2025-07-16 10:05:22 +08:00
this.innerHandler = handler.NewInnerHandler(this, this.HttpRpcClient)
//this.ExterServer.PostHandler("/server/detect", this.innerHandler.ServerDetect)
//
//this.ExterServer.GetHandler("/gate/setting/get", this.innerHandler.GateSettingGet)
//this.ExterServer.PostHandler("/gate/setting/save", this.innerHandler.GateSettingSave)
//
//this.ExterServer.GetHandler("/gate/cdkey/get", this.innerHandler.GateCdkeyGet)
//this.ExterServer.GetHandler("/gate/cdkey/list", this.innerHandler.GateCdkeyList)
//this.ExterServer.PostHandler("/gate/cdkey/add", this.innerHandler.GateCdkeyAdd)
//this.ExterServer.PostHandler("/gate/cdkey/generate", this.innerHandler.GateCdkeyGenerate)
//this.ExterServer.PostHandler("/gate/cdkey/delete", this.innerHandler.GateCdkeyDelete)
//
//this.ExterServer.GetHandler("/gate/user/query", this.innerHandler.GateUserQuery)
//this.ExterServer.PostHandler("/gate/user/operate", this.innerHandler.GateUserOperate)
//
//this.ExterServer.PostHandler("/game_1/data/reload", this.innerHandler.GameDataReload)
//
//this.ExterServer.GetHandler("/game_1/mail/list", this.innerHandler.GameMailList)
//this.ExterServer.PostHandler("/game_1/mail/send", this.innerHandler.GameMailSend)
//this.ExterServer.PostHandler("/game_1/mail/delete", this.innerHandler.GameMailDelete)
//
//this.ExterServer.GetHandler("/game_1/player/query", this.innerHandler.GamePlayerQuery)
//this.ExterServer.PostHandler("/game_1/player/operate", this.innerHandler.GamePlayerOperate)
2025-06-04 18:17:39 +08:00
return nil
}
func (this *adminServer) Serve() error {
2025-07-16 10:05:22 +08:00
return util.WaitAny(this.ExterServer.Serve, this.SubscribeClient.Work)
2025-06-04 18:17:39 +08:00
}
func (this *adminServer) Close() error {
this.Logger().Info("The 'SubscribeClient' is closing")
_ = this.SubscribeClient.Close()
2025-07-16 10:05:22 +08:00
this.Logger().Info("The 'ExterServer' is closing")
_ = this.ExterServer.Close()
2025-06-04 18:17:39 +08:00
this.Logger().Info("Server closed")
time.Sleep(time.Second)
2025-07-16 10:05:22 +08:00
_ = this.NamedServer.Close()
2025-06-04 18:17:39 +08:00
return nil
}