印度包网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.4 KiB

2 months ago
package customer
import (
"context"
"net/http"
"server/call"
"server/config"
"server/db"
edb "server/db/es"
mdb "server/db/mysql"
rdb "server/db/redis"
"server/modules/customer/bdb"
"server/modules/customer/routers"
"time"
"github.com/liangdas/mqant/conf"
"github.com/liangdas/mqant/log"
"github.com/liangdas/mqant/module"
basemodule "github.com/liangdas/mqant/module/base"
)
var (
Module = func() module.Module {
this := new(Customer)
return this
}
BackDB = new(mdb.MysqlClient)
)
type Customer struct {
basemodule.BaseModule
httpSvr *http.Server
addr string
}
func (b *Customer) GetType() string {
//很关键,需要与配置文件中的Module配置对应
return "customer"
}
func (b *Customer) Version() string {
//可以在监控时了解代码版本
return "1.0.0"
}
func (b *Customer) OnInit(app module.App, settings *conf.ModuleSettings) {
b.BaseModule.OnInit(b, app, settings)
call.NewCaller(b)
db.InitDB(&mdb.MysqlClient{}, &rdb.RedisClient{}, &edb.EsClient{})
bdb.InitMysql()
// 自动初始化后台数据库
bdb.MigrateDB()
// 加载配置
call.Go(func() {
loadConfig()
})
b.addr = config.GetConfig().Customer.Addr
call.NewSnowflake(int64(config.GetConfig().WorkID))
call.InitReload(b.App.Transport())
StartTimer()
}
func (b *Customer) startHttpServer() {
router := routers.SetUpRouter()
srv := &http.Server{
Addr: b.addr,
Handler: router,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}()
// returning reference so caller can call Shutdown()
b.httpSvr = srv
}
func (b *Customer) Run(closeSig chan bool) {
log.Info("customer: starting HTTP server :%s", b.addr)
call.InitTimeWheel(closeSig)
call.InitWarn(b.App.Transport())
b.startHttpServer()
<-closeSig
log.Info("customer: stopping HTTP server")
}
func (b *Customer) OnDestroy() {
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// now close the server gracefully ("shutdown")
// timeout could be given instead of nil as a https://golang.org/pkg/context/
if err := b.httpSvr.Shutdown(ctx); err != nil {
log.Error("OnDestroy Customer Shutdown error:%v", err)
}
log.Info("Customer: done. exiting")
//一定别忘了继承
b.BaseModule.OnDestroy()
log.Info("Customer 模块已销毁")
}