165 lines
5.2 KiB
Go
165 lines
5.2 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 gateServer struct {
|
|
server.NamedServer
|
|
|
|
InnerServer server.HttpServer
|
|
ExterServer server.HttpServer
|
|
|
|
dbConfig *db.Config
|
|
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) WithDbConfig(config *db.Config) {
|
|
this.dbConfig = config
|
|
}
|
|
|
|
func (this *gateServer) WithSdConfig(config *sd.Config) {
|
|
this.sdConfig = config
|
|
}
|
|
|
|
func (this *gateServer) Init() (err error) {
|
|
|
|
err = this.NamedServer.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)
|
|
}
|
|
|
|
this.MongoClient = db.NewMongoClient(this.dbConfig)
|
|
err = this.MongoClient.Init()
|
|
if err != nil {
|
|
return errors.Errorf("the 'MongoClient' init failed, %v", err)
|
|
}
|
|
|
|
this.RegisterClient = zk.NewRegisterClient(this, this.sdConfig, &this.InnerServer.Listener, nil)
|
|
err = this.RegisterClient.Init()
|
|
if err != nil {
|
|
return errors.Errorf("the 'RegisterClient' init failed, %v", err)
|
|
}
|
|
|
|
this.HttpRpcClient = rpc.NewHttpRpcClient(this.Logger())
|
|
err = this.HttpRpcClient.Init()
|
|
if err != nil {
|
|
return errors.Errorf("the 'HttpRpcClient' init failed, %v", err)
|
|
}
|
|
|
|
this.SubscribeClient = zk.NewSubscribeClient(this, this.sdConfig, map[string]sd.SubscribeCallback{
|
|
"user": this.HttpRpcClient.SubscribeCallback,
|
|
"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.HttpRpcClient, this.gateManager)
|
|
|
|
//register internal request handler
|
|
this.InnerServer.GetHandler("/setting/get", this.innerHandler.GetSetting)
|
|
this.InnerServer.PostHandler("/setting/set", this.innerHandler.SetSetting)
|
|
this.InnerServer.PostHandler("/notice/query", this.innerHandler.QueryNotice)
|
|
this.InnerServer.PostHandler("/notice/add", this.innerHandler.AddNotice)
|
|
this.InnerServer.PostHandler("/notice/delete", this.innerHandler.DeleteNotice)
|
|
|
|
//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.PostHandler("/logout", this.requestHandler.Login)
|
|
this.ExterServer.GetHandler("/captcha/phone", this.requestHandler.PhoneCaptcha)
|
|
this.ExterServer.GetHandler("/captcha/image", this.requestHandler.ImageCaptcha)
|
|
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.NamedServer.Close()
|
|
return nil
|
|
}
|