package pay import ( "context" "net/http" "server/call" "server/config" "server/db" edb "server/db/es" mdb "server/db/mysql" rdb "server/db/redis" "server/modules/pay/routers" "strconv" "time" "github.com/liangdas/mqant/conf" "github.com/liangdas/mqant/log" "github.com/liangdas/mqant/module" basemodule "github.com/liangdas/mqant/module/base" "github.com/liangdas/mqant/server" ) type Pay struct { basemodule.BaseModule httpSvr *http.Server } var Module = func() module.Module { this := new(Pay) return this } func (p *Pay) GetType() string { return "pay" } func (p *Pay) Version() string { //可以在监控时了解代码版本 return "1.0.0" } func (p *Pay) OnInit(app module.App, settings *conf.ModuleSettings) { metadata := make(map[string]string) metadata["workID"] = strconv.Itoa(int(config.GetConfig().WorkID)) p.BaseModule.OnInit(p, app, settings, server.RegisterInterval(5*time.Second), server.RegisterTTL(10*time.Second), server.Metadata(metadata), ) call.NewCaller(p) db.InitDB(&mdb.MysqlClient{}, &rdb.RedisClient{}, &edb.EsClient{}) log.Info("[%v]module init finish, config:%+v", p.GetType(), config.GetConfig().Pay) log.Info("[%v]module init finish, base:%+v", p.GetType(), config.GetBase()) call.InitReload(p.App.Transport()) initTimer() loadConfig() p.GetServer().RegisterGO("recharge", Recharge) p.GetServer().RegisterGO("withdraw", Withdraw) } func (p *Pay) Run(closeSig chan bool) { log.Info("[%v]module running", p.GetType()) call.InitTimeWheel(closeSig) p.startHttpServer() <-closeSig log.Info("[%v]module stop", p.GetType()) } func (p *Pay) startHttpServer() { cfg := config.GetConfig().Pay router := routers.SetUpRouter() srv := &http.Server{ Addr: cfg.Addr, Handler: router, } go func() { if config.GetConfig().Pay.TLS { if err := srv.ListenAndServeTLS(cfg.CertFile, cfg.KeyFile); err != nil { log.Error("ListenAndServeTLS fail error:%v", err) } } else { if err := srv.ListenAndServe(); err != nil { log.Error("ListenAndServe fail error:%v", err) } } }() // returning reference so caller can call Shutdown() p.httpSvr = srv } func (p *Pay) OnDestroy() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := p.httpSvr.Shutdown(ctx); err != nil { log.Error("OnDestroy Shutdown error:%v", err) } //一定别忘了继承 p.BaseModule.OnDestroy() log.Info("模块已销毁") }