-
Notifications
You must be signed in to change notification settings - Fork 1
/
global.go
100 lines (87 loc) · 2.2 KB
/
global.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package common
import (
"github.com/donech/tool/xdb"
"github.com/donech/tool/xjwt"
"github.com/donech/tool/xlog"
"github.com/donech/tool/xredis"
"github.com/go-redis/redis"
"github.com/jinzhu/gorm"
"github.com/spf13/viper"
)
//dbClient global dbClient
var dbClient *gorm.DB
//redisClient global redisClient
var redisClient *redis.Client
//InitGlobal init global vat and return a clean up func.
func InitGlobal() func() {
redisViper := viper.Sub("redis")
clean := func() {}
if redisViper == nil {
xlog.SS().Fatalf("init global redis error: not found config for redis in yaml")
return clean
}
redisCfg := xredis.Config{}
err := redisViper.Unmarshal(&redisCfg)
if err != nil {
xlog.SS().Fatalf("init global redis error: ", err)
return clean
}
rd := xredis.New(redisCfg)
RegisteGlobalRedis(rd)
dbViper := viper.Sub("db")
if dbViper == nil {
xlog.SS().Fatalf("init global db error: not found config for db in yaml")
return clean
}
dbCfg := xdb.Config{}
err = dbViper.Unmarshal(&dbCfg)
if err != nil {
xlog.SS().Fatalf("init global dbClient error: ", err)
}
db, clean := xdb.New(dbCfg)
RegisteGlobalDB(db)
return clean
}
//RegisteGlobalDB registe global dbClient
func RegisteGlobalDB(db *gorm.DB) {
dbClient = db
}
//RegisteGlobalRedis registe global redisClient
func RegisteGlobalRedis(client *redis.Client) {
redisClient = client
}
//GetDB get global dbClient
func GetDB() *gorm.DB {
return dbClient
}
//GetRedis get global redisClient
func GetRedis() *redis.Client {
return redisClient
}
var jwtFactory *xjwt.JWTFactory
func InitJwtFactory(login xjwt.LoginFunc) {
jwtViper := viper.Sub("jwt")
if jwtViper == nil {
xlog.SS().Fatalf("init global jwt factory error: not found config for jwt factory in yaml")
return
}
cfg := xjwt.Config{}
err := jwtViper.Unmarshal(&cfg)
if err != nil {
xlog.SS().Fatalf("init global jwt factory error: ", err)
return
}
xlog.SS().Debug("jwtConfig is ", cfg)
jf, err := xjwt.NewJWTFactory(cfg, xjwt.WithLoginFunc(login))
if err != nil {
xlog.SS().Fatalf("init global jwt factory error: ", err)
return
}
SetJwtFactory(&jf)
}
func SetJwtFactory(jf *xjwt.JWTFactory) {
jwtFactory = jf
}
func GetJwtFactory() *xjwt.JWTFactory {
return jwtFactory
}