165 lines
4.9 KiB
Go
165 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"ecs/servers/gate/handler"
|
|
"ecs/servers/gate/logic"
|
|
"github.com/oylshe1314/framework/client/db"
|
|
"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 {
|
|
guideConfig []*util.Pair[uint32, uint32]
|
|
}
|
|
|
|
func (this *ServerConfig) WithGuideConfig(guideConfig []*util.Pair[uint32, uint32]) {
|
|
this.guideConfig = guideConfig
|
|
}
|
|
|
|
func (this *ServerConfig) Init() (err error) {
|
|
return nil
|
|
}
|
|
|
|
type gateServer struct {
|
|
ServerConfig
|
|
|
|
server.LoggerServer
|
|
|
|
InnerServer server.HttpServer
|
|
ExterServer server.HttpServer
|
|
|
|
MongoClient db.MongoClient
|
|
|
|
SdConfig sd.Config
|
|
RegisterClient sd.RegisterClient
|
|
HttpRpcClient rpc.HttpRpcClient
|
|
SubscribeClient sd.SubscribeClient
|
|
|
|
gateManager *logic.GateManager
|
|
|
|
innerHandler *handler.InnerHandler
|
|
requestHandler *handler.RequestHandler
|
|
}
|
|
|
|
func NewGateServer() server.Server {
|
|
return &gateServer{}
|
|
}
|
|
|
|
func (this *gateServer) Init() (err error) {
|
|
|
|
err = this.LoggerServer.Init()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
this.InnerServer.SetServer(this)
|
|
err = this.InnerServer.Init()
|
|
if err != nil {
|
|
return errors.Errorf("the 'InnerServer' init failed, %v", err)
|
|
}
|
|
|
|
this.ExterServer.SetServer(this)
|
|
err = this.ExterServer.Init()
|
|
if err != nil {
|
|
return errors.Errorf("the 'ExterServer' init failed, %v", err)
|
|
}
|
|
|
|
err = this.MongoClient.Init()
|
|
if err != nil {
|
|
return errors.Errorf("the 'MongoClient' init failed, %v", err)
|
|
}
|
|
|
|
this.RegisterClient = zk.NewRegisterClient(&this.SdConfig)
|
|
this.RegisterClient.SetServer(this)
|
|
this.RegisterClient.SetListener(nil, &this.ExterServer.Listener)
|
|
err = this.RegisterClient.Init()
|
|
if err != nil {
|
|
return errors.Errorf("the 'RegisterClient' 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.SetServer(this)
|
|
this.SubscribeClient.AddSubscribe("user", this.HttpRpcClient.SubscribeCallback)
|
|
this.SubscribeClient.AddSubscribe("game", this.HttpRpcClient.SubscribeCallback)
|
|
err = this.SubscribeClient.Init()
|
|
if err != nil {
|
|
return errors.Errorf("the 'SubscribeClient' init failed, %v", err)
|
|
}
|
|
|
|
this.gateManager = logic.NewGateManager(this, &this.MongoClient, &this.HttpRpcClient)
|
|
err = server.InitManagers(this.gateManager)
|
|
if err != nil {
|
|
return errors.Errorf("the manager init failed, %v", err)
|
|
}
|
|
|
|
this.innerHandler = handler.NewInnerHandler(this, this.gateManager)
|
|
this.requestHandler = handler.NewRequestHandler(this, this.gateManager, &this.HttpRpcClient)
|
|
|
|
//register internal request handler
|
|
this.InnerServer.GetHandler("/setting/get", this.innerHandler.GetSetting)
|
|
this.InnerServer.PostHandler("/setting/save", this.innerHandler.SaveSetting)
|
|
|
|
//this.InnerServer.GetHandler("/user/query", this.innerHandler.UserQuery)
|
|
//this.InnerServer.PostHandler("/user/operate", this.innerHandler.UserOperate)
|
|
//
|
|
//this.InnerServer.GetHandler("/cdkey/get", this.innerHandler.CdkeyGet)
|
|
//this.InnerServer.GetHandler("/cdkey/list", this.innerHandler.CdkeyList)
|
|
//this.InnerServer.PostHandler("/cdkey/add", this.innerHandler.CdkeyAdd)
|
|
//this.InnerServer.PostHandler("/cdkey/generate", this.innerHandler.CdkeyGenerate)
|
|
//this.InnerServer.PostHandler("/cdkey/delete", this.innerHandler.CdkeyDelete)
|
|
//
|
|
//this.InnerServer.PostHandler("/token/verify", this.innerHandler.TokenVerify)
|
|
|
|
//register external request handler
|
|
this.ExterServer.PostHandler("/signUp", this.requestHandler.SignUp)
|
|
this.ExterServer.PostHandler("/login", this.requestHandler.Login)
|
|
this.ExterServer.GetHandler("/notice", this.requestHandler.Notice)
|
|
|
|
//
|
|
//this.ExterServer.GetHandler("/callback/charge/wechat/ios", this.requestHandler.IOSWechatChargeCallback)
|
|
//this.ExterServer.GetHandler("/callback/accessTokenExpired/wechat/ios", this.requestHandler.IOSWechatAccessTokenExpiredCallback)
|
|
//
|
|
//this.ExterServer.PostHandler("/callback/charge/quickGame", this.requestHandler.QuickGameChargeCallback)
|
|
|
|
return
|
|
}
|
|
|
|
func (this *gateServer) Serve() (err error) {
|
|
return util.WaitAny(this.InnerServer.Serve, this.ExterServer.Serve, this.RegisterClient.Work, this.SubscribeClient.Work)
|
|
}
|
|
|
|
func (this *gateServer) Close() error {
|
|
|
|
this.Logger().Info("The 'InnerServer' is closing")
|
|
_ = this.InnerServer.Close()
|
|
|
|
this.Logger().Info("The 'ExterServer' is closing")
|
|
_ = this.ExterServer.Close()
|
|
|
|
this.Logger().Info("The 'RegisterClient' is closing")
|
|
_ = this.RegisterClient.Close()
|
|
|
|
this.Logger().Info("The 'SubscribeClient' is closing")
|
|
_ = this.SubscribeClient.Close()
|
|
|
|
this.Logger().Info("The 'HttpRpcClient' is closing")
|
|
_ = this.HttpRpcClient.Close()
|
|
|
|
this.Logger().Info("Server closed")
|
|
|
|
time.Sleep(time.Second)
|
|
_ = this.LoggerServer.Close()
|
|
return nil
|
|
}
|