|
@@ -4,8 +4,8 @@ import (
|
|
|
"context"
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
redisLib "github.com/go-redis/redis/v8"
|
|
|
- "github.com/gogf/gf/frame/g"
|
|
|
- "github.com/gogf/gf/net/ghttp"
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
|
+ "github.com/gogf/gf/v2/net/ghttp"
|
|
|
"go.uber.org/dig"
|
|
|
"gorm.io/gorm"
|
|
|
"gxt-api-frame/app/bll/impl"
|
|
@@ -25,24 +25,24 @@ const VERSION = "1.0.0"
|
|
|
|
|
|
func init() {
|
|
|
// 初始化logger
|
|
|
- logger.Init(g.Cfg().GetString("common.run_mode"))
|
|
|
+ logger.Init(utils.GetConfig("common.run_mode").String())
|
|
|
logger.SetVersion(VERSION)
|
|
|
logger.SetTraceIdFunc(utils.NewTraceID)
|
|
|
ctx := logger.NewTraceIDContext(context.Background(), utils.NewTraceID())
|
|
|
Init(ctx)
|
|
|
}
|
|
|
|
|
|
-// 初始化App,
|
|
|
+// Init 初始化App,
|
|
|
// TODO: 返回释放回调,暂时没调用
|
|
|
func Init(ctx context.Context) func() {
|
|
|
- logger.Printf(ctx, "服务启动,运行模式:%s,版本号:%s,进程号:%d", g.Cfg().Get("common.run_mode"), VERSION, os.Getpid())
|
|
|
+ logger.Printf(ctx, "服务启动,运行模式:%s,版本号:%s,进程号:%d", utils.GetConfig("common.run_mode").String(), VERSION, os.Getpid())
|
|
|
// 初始化依赖注入容器
|
|
|
container, call := buildContainer(ctx)
|
|
|
// 初始化路由注册
|
|
|
s := g.Server()
|
|
|
// 每个请求生成新的追踪Id,如果上下文件中没有trace-id
|
|
|
s.Use(middleware.TraceIdMiddleware())
|
|
|
- if g.Cfg().GetBool("prometheus.enable") {
|
|
|
+ if utils.GetConfig("prometheus.enable").Bool() {
|
|
|
p := middleware.NewPrometheus("ghttp")
|
|
|
p.Use(s)
|
|
|
}
|
|
@@ -66,16 +66,16 @@ func Init(ctx context.Context) func() {
|
|
|
// 初始化jwt认证,可以把相关配置放到config.toml中
|
|
|
func initJwtAuth(ctx context.Context, container *dig.Container) error {
|
|
|
var opts []auth.Option
|
|
|
- opts = append(opts, auth.SetExpired(g.Cfg().GetInt("jwt.expired")))
|
|
|
- opts = append(opts, auth.SetSigningKey([]byte(g.Cfg().GetString("jwt.signing_key"))))
|
|
|
+ opts = append(opts, auth.SetExpired(utils.GetConfig("jwt.expired").Int()))
|
|
|
+ opts = append(opts, auth.SetSigningKey([]byte(utils.GetConfig("jwt.signing_key").String())))
|
|
|
opts = append(opts, auth.SetKeyfunc(func(t *jwt.Token) (interface{}, error) {
|
|
|
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
|
return nil, errors.ErrInvalidToken
|
|
|
}
|
|
|
- return []byte(g.Cfg().GetString("jwt.signing_key")), nil
|
|
|
+ return []byte(utils.GetConfig("jwt.signing_key").String()), nil
|
|
|
}))
|
|
|
|
|
|
- switch g.Cfg().GetString("jwt.signing_method") {
|
|
|
+ switch utils.GetConfig("jwt.signing_method").String() {
|
|
|
case "HS256":
|
|
|
opts = append(opts, auth.SetSigningMethod(jwt.SigningMethodHS256))
|
|
|
case "HS384":
|
|
@@ -88,9 +88,9 @@ func initJwtAuth(ctx context.Context, container *dig.Container) error {
|
|
|
|
|
|
// 初始化redis
|
|
|
func initRedis(ctx context.Context, container *dig.Container) func() {
|
|
|
- addr := g.Cfg().GetString("redis.addr")
|
|
|
- password := g.Cfg().GetString("redis.password")
|
|
|
- db := g.Cfg().GetInt("redis.db")
|
|
|
+ addr := utils.GetConfig("redis.addr").String()
|
|
|
+ password := utils.GetConfig("redis.password").String()
|
|
|
+ db := utils.GetConfig("redis.db").Int()
|
|
|
redisCli := redis.Init(ctx, addr, password, db)
|
|
|
logger.Printf(ctx, "REDIS初始化成功,当前服务器[%s]", addr)
|
|
|
// 注入redis client
|
|
@@ -110,7 +110,7 @@ func initStore(ctx context.Context, container *dig.Container) (func(), error) {
|
|
|
return storeCall, err
|
|
|
}
|
|
|
// 如果自动映射数据表
|
|
|
- if g.Cfg().GetBool("gorm.enable_auto_migrate") {
|
|
|
+ if utils.GetConfig("gorm.enable_auto_migrate").Bool() {
|
|
|
err = autoMigrate(db)
|
|
|
if err != nil {
|
|
|
return storeCall, err
|
|
@@ -125,8 +125,8 @@ func initStore(ctx context.Context, container *dig.Container) (func(), error) {
|
|
|
_ = sqlDb.Close()
|
|
|
}
|
|
|
logger.Printf(ctx, "MYSQL初始化成功, 服务器[%s], 数据库[%s]",
|
|
|
- g.Cfg().GetString("mysql.host"),
|
|
|
- g.Cfg().GetString("mysql.db_name"))
|
|
|
+ utils.GetConfig("mysql.host").String(),
|
|
|
+ utils.GetConfig("mysql.db_name").String())
|
|
|
return storeCall, nil
|
|
|
}
|
|
|
|
|
@@ -140,7 +140,7 @@ func buildContainer(ctx context.Context) (*dig.Container, func()) {
|
|
|
}
|
|
|
// 初始化redis模块
|
|
|
var redisCall func()
|
|
|
- if g.Cfg().GetBool("redis.enable") {
|
|
|
+ if utils.GetConfig("redis.enable").Bool() {
|
|
|
redisCall = initRedis(ctx, container)
|
|
|
}
|
|
|
// 注入bll
|