2022-11-24 23:37:34 +08:00
|
|
|
|
// Package global
|
|
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
|
|
|
|
// @Copyright Copyright (c) 2022 HotGo CLI
|
|
|
|
|
// @Author Ms <133814250@qq.com>
|
|
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
|
|
|
//
|
|
|
|
|
package global
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2022-12-11 20:48:18 +08:00
|
|
|
|
"github.com/gogf/gf/v2"
|
2023-01-25 11:49:21 +08:00
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
2023-01-18 16:23:39 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"github.com/gogf/gf/v2/os/gcache"
|
2023-01-25 11:49:21 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2023-01-25 11:49:21 +08:00
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2022-12-11 20:48:18 +08:00
|
|
|
|
"hotgo/internal/consts"
|
2023-01-18 16:23:39 +08:00
|
|
|
|
"hotgo/internal/library/hggen"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"hotgo/internal/library/location"
|
2023-01-25 11:49:21 +08:00
|
|
|
|
"hotgo/internal/library/queue"
|
|
|
|
|
"hotgo/internal/model/entity"
|
|
|
|
|
"hotgo/internal/service"
|
|
|
|
|
"hotgo/utility/charset"
|
2022-11-25 23:22:44 +08:00
|
|
|
|
"hotgo/utility/simple"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Init(ctx context.Context) {
|
2023-01-25 11:49:21 +08:00
|
|
|
|
_, err := g.Cfg().Get(ctx, "hotgo.debug")
|
|
|
|
|
if err != nil {
|
2023-01-18 16:23:39 +08:00
|
|
|
|
g.Log().Fatal(ctx, "配置读取异常:", err, "\r\n你确定 config/config.yaml 文件存在且格式正确吗?\r\n")
|
2023-01-25 11:49:21 +08:00
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
2023-01-25 11:49:21 +08:00
|
|
|
|
//g.SetDebug(debug.Bool())
|
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// 默认上海时区
|
|
|
|
|
if err := gtime.SetTimeZone("Asia/Shanghai"); err != nil {
|
2023-01-18 16:23:39 +08:00
|
|
|
|
g.Log().Fatalf(ctx, "时区设置异常err:%+v", err)
|
2022-11-24 23:37:34 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RootPtah, _ = os.Getwd()
|
2022-12-11 20:48:18 +08:00
|
|
|
|
fmt.Printf("欢迎使用HotGo!\r\n当前运行环境:%v, 运行根路径为:%v \r\nHotGo版本:v%v, gf版本:%v \n", SysType, RootPtah, consts.VersionApp, gf.VERSION)
|
2023-01-18 16:23:39 +08:00
|
|
|
|
|
2023-01-25 11:49:21 +08:00
|
|
|
|
g.Log().SetHandlers(LoggingServeLogHandler)
|
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
|
setOrmCacheAdapter()
|
|
|
|
|
|
2023-01-25 11:49:21 +08:00
|
|
|
|
service.SysBlacklist().Load(ctx)
|
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
|
startMonitor(ctx)
|
|
|
|
|
|
|
|
|
|
hggen.InIt(ctx)
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
|
func startMonitor(ctx context.Context) {
|
2022-11-25 23:22:44 +08:00
|
|
|
|
simple.SafeGo(ctx, func(ctx context.Context) {
|
2022-11-24 23:37:34 +08:00
|
|
|
|
MonitorData.STartTime = gtime.Now()
|
2023-01-18 16:23:39 +08:00
|
|
|
|
intranetIP, err := location.GetLocalIP()
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Log().Warningf(ctx, "parse intranetIP err:%+v", err)
|
|
|
|
|
}
|
|
|
|
|
MonitorData.IntranetIP = intranetIP
|
|
|
|
|
|
|
|
|
|
publicIP, err := location.GetPublicIP(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Log().Warningf(ctx, "parse publicIP err:%+v", err)
|
|
|
|
|
}
|
|
|
|
|
MonitorData.PublicIP = publicIP
|
2022-11-24 23:37:34 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
2023-01-18 16:23:39 +08:00
|
|
|
|
|
|
|
|
|
func setOrmCacheAdapter() {
|
|
|
|
|
redisCache := gcache.NewAdapterRedis(g.Redis())
|
|
|
|
|
g.DB().GetCache().SetAdapter(redisCache)
|
|
|
|
|
}
|
2023-01-25 11:49:21 +08:00
|
|
|
|
|
|
|
|
|
func LoggingServeLogHandler(ctx context.Context, in *glog.HandlerInput) {
|
|
|
|
|
in.Next(ctx)
|
|
|
|
|
|
|
|
|
|
conf, err := service.SysConfig().GetLoadServeLog(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !conf.Switch {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if in.LevelFormat == "" || !gstr.InArray(conf.LevelFormat, in.LevelFormat) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data entity.SysServeLog
|
|
|
|
|
data.TraceId = gctx.CtxId(ctx)
|
|
|
|
|
data.LevelFormat = in.LevelFormat
|
|
|
|
|
data.Content = in.Content
|
|
|
|
|
data.Stack = gjson.New(charset.ParseStack(in.Stack))
|
|
|
|
|
data.Line = in.CallerPath
|
|
|
|
|
data.TriggerNs = in.Time.UnixNano()
|
|
|
|
|
data.Status = consts.StatusEnabled
|
|
|
|
|
|
|
|
|
|
if data.Stack.IsNil() {
|
|
|
|
|
data.Stack = gjson.New(consts.NilJsonToString)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if gstr.Contains(in.Content, `exception recovered`) {
|
|
|
|
|
data.LevelFormat = "PANI"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if conf.Queue {
|
|
|
|
|
err = queue.Push(consts.QueueServeLogTopic, data)
|
|
|
|
|
} else {
|
|
|
|
|
err = service.SysServeLog().RealWrite(ctx, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Log().Printf(ctx, "LoggingServeLogHandler err:%+v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|