ecs/servers/user/server.go

102 lines
2.4 KiB
Go
Raw Permalink Normal View History

2025-06-04 18:17:39 +08:00
package main
import (
"ecs/servers/user/handler"
"ecs/servers/user/logic"
"github.com/oylshe1314/framework/client/db"
"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 userServer struct {
2025-07-16 10:05:22 +08:00
server.NamedServer
2025-06-04 18:17:39 +08:00
InnerServer 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
userManager *logic.UserManager
innerHandler *handler.InnerHandler
}
func NewUserServer() server.Server {
return &userServer{}
}
2025-07-16 10:05:22 +08:00
func (this *userServer) WithDbConfig(config *db.Config) {
this.dbConfig = config
}
func (this *userServer) WithSdConfig(config *sd.Config) {
this.sdConfig = config
}
2025-06-04 18:17:39 +08:00
func (this *userServer) 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)
}
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.userManager = logic.NewUserManager(this, this.MongoClient)
2025-06-04 18:17:39 +08:00
err = this.userManager.Init()
if err != nil {
return errors.Errorf("the 'userManager' init failed, %v", err)
}
this.innerHandler = handler.NewInnerHandler(this, this.userManager)
//register internal request handler
this.InnerServer.GetHandler("/signUp", this.innerHandler.SignUp)
this.InnerServer.PostHandler("/login", this.innerHandler.Login)
this.InnerServer.PostHandler("/verify/token", this.innerHandler.TokenVerify)
return
}
func (this *userServer) Serve() (err error) {
return util.WaitAny(this.InnerServer.Serve, this.RegisterClient.Work)
}
func (this *userServer) Close() error {
this.Logger().Info("The 'InnerServer' is closing")
_ = this.InnerServer.Close()
this.Logger().Info("The 'RegisterClient' is closing")
_ = this.RegisterClient.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
}