hotgo/server/internal/global/init.go

126 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package global
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package global
import (
"context"
"fmt"
"github.com/gogf/gf/v2"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/text/gstr"
"hotgo/internal/consts"
"hotgo/internal/library/cache"
"hotgo/internal/library/hggen"
"hotgo/internal/library/queue"
"hotgo/internal/model/entity"
"hotgo/internal/service"
"hotgo/utility/charset"
"strings"
)
func Init(ctx context.Context) {
// 设置服务日志处理
g.Log().SetHandlers(LoggingServeLogHandler)
if _, err := g.Cfg().Get(ctx, "hotgo.debug"); err != nil {
g.Log().Fatal(ctx, "配置读取异常:", err, "\r\n你确定 config/config.yaml 文件存在且格式正确吗?\r\n")
return
}
// 默认上海时区
if err := gtime.SetTimeZone("Asia/Shanghai"); err != nil {
g.Log().Fatalf(ctx, "时区设置异常 err%+v", err)
return
}
RootPtah = gfile.Pwd()
fmt.Printf("欢迎使用HotGo\r\n当前运行环境%v, 运行根路径为:%v \r\nHotGo版本v%v, gf版本%v \n", SysType, RootPtah, consts.VersionApp, gf.VERSION)
// 设置缓存适配器
cache.SetAdapter(ctx)
// 启动服务监控
service.AdminMonitor().StartMonitor(ctx)
// 加载ip访问黑名单
service.SysBlacklist().Load(ctx)
// 初始化功能库配置
service.SysConfig().InitConfig(ctx)
// 注册支付成功回调方法
payNotifyCall()
// 初始化生成代码配置
hggen.InIt(ctx)
}
func LoggingServeLogHandler(ctx context.Context, in *glog.HandlerInput) {
in.Next(ctx)
err := g.Try(ctx, func(ctx context.Context) {
var err error
defer func() {
if err != nil {
panic(err)
}
}()
conf, err := service.SysConfig().GetLoadServeLog(ctx)
if err != nil {
return
}
if conf == nil {
return
}
if !conf.Switch {
return
}
if in.LevelFormat == "" || !gstr.InArray(conf.LevelFormat, in.LevelFormat) {
return
}
if in.Stack == "" {
in.Stack = in.Logger.GetStack(4) // 4是跳过当前方法如果调整本行位置需要重新调整skip
}
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 = strings.TrimRight(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("serveLog").Errorf(ctx, "LoggingServeLogHandler err:%+v", err)
}
}