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.
141 lines
3.2 KiB
141 lines
3.2 KiB
package web |
|
|
|
import ( |
|
"context" |
|
"math/rand" |
|
"net/http" |
|
"server/call" |
|
"server/config" |
|
"server/db" |
|
edb "server/db/es" |
|
mdb "server/db/mysql" |
|
rdb "server/db/redis" |
|
"server/modules/web/routers" |
|
"server/util" |
|
"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(Web) |
|
return this |
|
} |
|
|
|
type Web struct { |
|
basemodule.BaseModule |
|
// addr string |
|
|
|
httpSvr *http.Server |
|
// httpSvr2 *http.Server |
|
} |
|
|
|
func (w *Web) GetType() string { |
|
// 很关键,需要与配置文件中的Module配置对应 |
|
return "web" |
|
} |
|
func (w *Web) Version() string { |
|
// 可以在监控时了解代码版本 |
|
return "1.0.0" |
|
} |
|
func (w *Web) OnInit(app module.App, settings *conf.ModuleSettings) { |
|
w.BaseModule.OnInit(w, app, settings) |
|
|
|
db.InitDB(&rdb.RedisClient{}, &edb.EsClient{}, &mdb.MysqlClient{}) |
|
log.Info("[%v]module init finish, config:%+v", w.GetType(), config.GetConfig().Web) |
|
log.Info("[%v]module init finish, base:%+v", w.GetType(), config.GetBase()) |
|
|
|
// 自动初始化数据库 |
|
// MigrateDB() |
|
call.NewCaller(w) |
|
|
|
call.NewSnowflake(int64(config.GetConfig().WorkID)) |
|
|
|
// 创建一个随机数生成器 |
|
rand.Seed(time.Now().UnixNano()) |
|
|
|
// 后台改配置 |
|
if err := loadConfig(); err != nil { |
|
log.Error("err:%v", err) |
|
panic(err) |
|
} |
|
|
|
call.InitReload(w.App.Transport()) |
|
if err := call.LoadIpDB(); err != nil { |
|
log.Error("err:%v", err) |
|
panic(err) |
|
} |
|
|
|
// 拉取缓存数据 |
|
util.Go(func() { |
|
FetchDatas() |
|
}) |
|
} |
|
|
|
func (wb *Web) startHttpServer() { |
|
webcfg := config.GetConfig().Web |
|
router := routers.SetUpRouter() |
|
srv := &http.Server{ |
|
Addr: webcfg.Addr, |
|
Handler: router, |
|
} |
|
|
|
go func() { |
|
if config.GetConfig().Web.TLS { |
|
if err := srv.ListenAndServeTLS(webcfg.CertFile, webcfg.KeyFile); err != nil { |
|
log.Error("web ListenAndServeTLS fail error:%v", err) |
|
} |
|
} else { |
|
if err := srv.ListenAndServe(); err != nil { |
|
log.Error("web ListenAndServe fail error:%v", err) |
|
} |
|
} |
|
|
|
}() |
|
// srv2 := &http.Server{ |
|
// Addr: ":80", |
|
// Handler: router, |
|
// } |
|
// go func() { |
|
// if err := srv2.ListenAndServe(); err != nil { |
|
// log.Error("web ListenAndServe fail error:%v", err) |
|
// } |
|
// }() |
|
// returning reference so caller can call Shutdown() |
|
wb.httpSvr = srv |
|
// wb.httpSvr2 = srv2 |
|
} |
|
|
|
func (w *Web) Run(closeSig chan bool) { |
|
log.Info("web: starting HTTP server :%s", config.GetConfig().Web.Addr) |
|
w.startHttpServer() |
|
<-closeSig |
|
log.Info("web: stopping HTTP server") |
|
|
|
} |
|
|
|
func (w *Web) 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 := w.httpSvr.Shutdown(ctx); err != nil { |
|
log.Error("OnDestroy web Shutdown error:%v", err) |
|
} |
|
// if err := w.httpSvr2.Shutdown(ctx); err != nil { |
|
// log.Error("OnDestroy web Shutdown error:%v", err) |
|
// } |
|
|
|
log.Info("web: done. exiting") |
|
|
|
// 一定别忘了继承 |
|
w.BaseModule.OnDestroy() |
|
|
|
log.Info("web 模块已销毁") |
|
}
|
|
|