ecs/servers/gate/server.go

165 lines
5.2 KiB
Go
Raw Permalink Normal View History

2025-06-04 18:17:39 +08:00
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 {
2025-07-16 10:05:22 +08:00
server.NamedServer
2025-06-04 18:17:39 +08:00
InnerServer server.HttpServer
ExterServer server.HttpServer
2025-07-16 10:05:22 +08:00
dbConfig *db.Config
2025-06-04 18:17:39 +08:00
MongoClient db.MongoClient
2025-07-16 10:05:22 +08:00
sdConfig *sd.Config
2025-06-04 18:17:39 +08:00
RegisterClient sd.RegisterClient
HttpRpcClient rpc.HttpRpcClient
SubscribeClient sd.SubscribeClient
gateManager *logic.GateManager
innerHandler *handler.InnerHandler
requestHandler *handler.RequestHandler
}
func NewGateServer() server.Server {
return &gateServer{}
}
2025-07-16 10:05:22 +08:00
func (this *gateServer) WithDbConfig(config *db.Config) {
this.dbConfig = config
}
func (this *gateServer) WithSdConfig(config *sd.Config) {
this.sdConfig = config
}
2025-06-04 18:17:39 +08:00
func (this *gateServer) Init() (err error) {
2025-07-16 10:05:22 +08:00
err = this.NamedServer.Init()
2025-06-04 18:17:39 +08:00
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)
}
2025-07-16 10:05:22 +08:00
this.MongoClient = db.NewMongoClient(this.dbConfig)
2025-06-04 18:17:39 +08:00
err = this.MongoClient.Init()
if err != nil {
return errors.Errorf("the 'MongoClient' init failed, %v", err)
}
2025-07-16 10:05:22 +08:00
this.RegisterClient = zk.NewRegisterClient(this, this.sdConfig, &this.InnerServer.Listener, nil)
2025-06-04 18:17:39 +08:00
err = this.RegisterClient.Init()
if err != nil {
return errors.Errorf("the 'RegisterClient' init failed, %v", err)
}
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
this.SubscribeClient = zk.NewSubscribeClient(this, this.sdConfig, map[string]sd.SubscribeCallback{
"user": this.HttpRpcClient.SubscribeCallback,
"game": this.HttpRpcClient.SubscribeCallback,
})
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.gateManager = logic.NewGateManager(this, this.MongoClient, this.HttpRpcClient)
2025-06-04 18:17:39 +08:00
err = server.InitManagers(this.gateManager)
if err != nil {
return errors.Errorf("the manager init failed, %v", err)
}
this.innerHandler = handler.NewInnerHandler(this, this.gateManager)
2025-07-16 10:05:22 +08:00
this.requestHandler = handler.NewRequestHandler(this, this.HttpRpcClient, this.gateManager)
2025-06-04 18:17:39 +08:00
//register internal request handler
this.InnerServer.GetHandler("/setting/get", this.innerHandler.GetSetting)
2025-07-17 16:21:08 +08:00
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)
2025-06-04 18:17:39 +08:00
//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)
2025-07-17 16:21:08 +08:00
this.ExterServer.PostHandler("/logout", this.requestHandler.Login)
this.ExterServer.GetHandler("/captcha/phone", this.requestHandler.PhoneCaptcha)
this.ExterServer.GetHandler("/captcha/image", this.requestHandler.ImageCaptcha)
2025-06-04 18:17:39 +08:00
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)
2025-07-16 10:05:22 +08:00
_ = this.NamedServer.Close()
2025-06-04 18:17:39 +08:00
return nil
}