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 { serverSubscribe []string } func (this *ServerConfig) WithServerSubscribe(serverSubscribe []string) { this.serverSubscribe = serverSubscribe } func (this *ServerConfig) Init() (err error) { if len(this.serverSubscribe) == 0 { return errors.Error("'serverSubscribe' can not be empty") } return nil } type adminServer struct { ServerConfig server.LoggerServer InnerServer server.HttpServer HttpRpcClient rpc.HttpRpcClient SdConfig sd.Config SubscribeClient sd.SubscribeClient innerHandler *handler.InnerHandler } func NewAdminServer() server.Server { return &adminServer{} } func (this *adminServer) Init() (err error) { err = this.ServerConfig.Init() if err != nil { return err } err = this.LoggerServer.Init() if err != nil { return err } this.InnerServer.SetLogger(this.Logger()) err = this.InnerServer.Init() if err != nil { return errors.Errorf("the 'InnerServer' init failed, %v", err) } this.HttpRpcClient.SetLogger(this.Logger()) err = this.HttpRpcClient.Init() if err != nil { return errors.Errorf("the 'HttpRpcClient' init failed, %v", err) } this.SubscribeClient = zk.NewSubscribeClient(&this.SdConfig) this.SubscribeClient.SetLogger(this.Logger()) for _, subSvr := range this.serverSubscribe { this.SubscribeClient.AddSubscribe(subSvr, this.HttpRpcClient.SubscribeCallback) } err = this.SubscribeClient.Init() if err != nil { return errors.Errorf("the 'SubscribeClient' init failed, %v", err) } this.innerHandler = handler.NewInnerHandler(this, &this.HttpRpcClient) this.InnerServer.PostHandler("/server/detect", this.innerHandler.ServerDetect) this.InnerServer.GetHandler("/gate/setting/get", this.innerHandler.GateSettingGet) this.InnerServer.PostHandler("/gate/setting/save", this.innerHandler.GateSettingSave) this.InnerServer.GetHandler("/gate/cdkey/get", this.innerHandler.GateCdkeyGet) this.InnerServer.GetHandler("/gate/cdkey/list", this.innerHandler.GateCdkeyList) this.InnerServer.PostHandler("/gate/cdkey/add", this.innerHandler.GateCdkeyAdd) this.InnerServer.PostHandler("/gate/cdkey/generate", this.innerHandler.GateCdkeyGenerate) this.InnerServer.PostHandler("/gate/cdkey/delete", this.innerHandler.GateCdkeyDelete) this.InnerServer.GetHandler("/gate/user/query", this.innerHandler.GateUserQuery) this.InnerServer.PostHandler("/gate/user/operate", this.innerHandler.GateUserOperate) this.InnerServer.PostHandler("/game_1/data/reload", this.innerHandler.GameDataReload) this.InnerServer.GetHandler("/game_1/mail/list", this.innerHandler.GameMailList) this.InnerServer.PostHandler("/game_1/mail/send", this.innerHandler.GameMailSend) this.InnerServer.PostHandler("/game_1/mail/delete", this.innerHandler.GameMailDelete) this.InnerServer.GetHandler("/game_1/player/query", this.innerHandler.GamePlayerQuery) this.InnerServer.PostHandler("/game_1/player/operate", this.innerHandler.GamePlayerOperate) return nil } func (this *adminServer) Serve() error { return util.WaitAny(this.SubscribeClient.Work, this.InnerServer.Serve) } func (this *adminServer) Close() error { this.Logger().Info("The 'SubscribeClient' is closing") _ = this.SubscribeClient.Close() this.Logger().Info("The 'InnerServer' is closing") _ = this.InnerServer.Close() this.Logger().Info("Server closed") time.Sleep(time.Second) _ = this.LoggerServer.Close() return nil }