/
init.go
171 lines (137 loc) · 4.25 KB
/
init.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package src
import (
"database/sql"
"fmt"
"os"
"time"
"github.com/LyricTian/gin-admin/src/logger"
model "github.com/LyricTian/gin-admin/src/model/mysql"
"github.com/LyricTian/gin-admin/src/service/mysql"
"github.com/LyricTian/gin-admin/src/util"
"github.com/LyricTian/gin-admin/src/web"
"github.com/LyricTian/gin-admin/src/web/ctl"
"github.com/LyricTian/logrus-mysql-hook"
"github.com/casbin/casbin"
"github.com/facebookgo/inject"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
// CloseHandle 关闭服务
type CloseHandle func()
// Init 初始化所有服务
func Init(version, traceID string) (*gin.Engine, CloseHandle) {
// 初始化MySQL
db := InitMySQL()
// 初始化日志
loggerHook := InitLogger(db.Db)
logger.System(traceID).Infof("服务已运行在[%s]模式下,版本号:%s,进程号:%d",
viper.GetString("run_mode"), version, os.Getpid())
// 初始化依赖注入
enforcer, _, ctlCommon := InitInject(db)
// 初始化HTTP服务
httpHandler := web.Init(db, enforcer, ctlCommon)
return httpHandler, func() {
// 等待日志钩子写入完成
if loggerHook != nil {
loggerHook.Flush()
}
// 关闭数据库
err := db.Close()
if err != nil {
logger.System(traceID).Errorf("关闭数据库发生错误: %s", err.Error())
}
}
}
// InitInject 初始化依赖注入
func InitInject(db *mysql.DB) (*casbin.Enforcer, *model.Common, *ctl.Common) {
g := new(inject.Graph)
// 注入casbin
enforcer := casbin.NewEnforcer(viper.GetString("casbin_model_conf"), false)
g.Provide(&inject.Object{Value: enforcer})
// 注入mysql存储
modelCommom := new(model.Common).Init(g, db)
// 注入控制器
ctlCommon := new(ctl.Common)
g.Provide(&inject.Object{Value: ctlCommon})
if err := g.Populate(); err != nil {
panic("注入模块发生错误:" + err.Error())
}
return enforcer, modelCommom, ctlCommon
}
// InitMySQL 初始化mysql数据库
func InitMySQL() *mysql.DB {
mysqlConfig := viper.GetStringMap("mysql")
var opts []mysql.Option
if v := util.T(mysqlConfig["trace"]).Bool(); v {
opts = append(opts, mysql.SetTrace(v))
}
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s",
mysqlConfig["username"],
mysqlConfig["password"],
mysqlConfig["addr"],
mysqlConfig["database"],
)
opts = append(opts, mysql.SetDSN(dsn))
if v := util.T(mysqlConfig["engine"]).String(); v != "" {
opts = append(opts, mysql.SetEngine(v))
}
if v := util.T(mysqlConfig["encoding"]).String(); v != "" {
opts = append(opts, mysql.SetEncoding(v))
}
if v := util.T(mysqlConfig["max_lifetime"]).Int(); v > 0 {
opts = append(opts, mysql.SetMaxLifetime(time.Duration(v)*time.Second))
}
if v := util.T(mysqlConfig["max_open_conns"]).Int(); v > 0 {
opts = append(opts, mysql.SetMaxOpenConns(v))
}
if v := util.T(mysqlConfig["max_idle_conns"]).Int(); v > 0 {
opts = append(opts, mysql.SetMaxIdleConns(v))
}
db, err := mysql.NewDB(opts...)
if err != nil {
panic("初始化MySQL数据库发生错误:" + err.Error())
}
return db
}
// InitLogger 初始化日志
func InitLogger(mysqlDB *sql.DB) logger.HookFlusher {
logConfig := viper.GetStringMap("log")
var opts []logger.Option
if v := util.T(logConfig["level"]).Int(); v > 0 {
opts = append(opts, logger.SetLevel(v))
}
if v := util.T(logConfig["format"]).String(); v != "" {
opts = append(opts, logger.SetFormat(v))
}
l := logger.New(opts...)
if v := util.T(logConfig["hook"]).String(); v != "" {
switch v {
case "mysql":
extraItems := []*mysqlhook.ExecExtraItem{
mysqlhook.NewExecExtraItem(logger.FieldKeyType, "varchar(20)"),
mysqlhook.NewExecExtraItem(logger.FieldKeyUserID, "varchar(36)"),
mysqlhook.NewExecExtraItem(logger.FieldKeyTraceID, "varchar(36)"),
}
var hookOpts []mysqlhook.Option
hookConfig := viper.GetStringMap("log-mysql-hook")
if v := util.T(hookConfig["max_buffer"]).Int(); v > 0 {
hookOpts = append(hookOpts, mysqlhook.SetMaxQueues(v))
}
if v := util.T(hookConfig["max_thread"]).Int(); v > 0 {
hookOpts = append(hookOpts, mysqlhook.SetMaxWorkers(v))
}
hook := mysqlhook.DefaultWithExtra(
mysqlDB,
fmt.Sprintf("%s_%s",
viper.GetStringMap("mysql")["table_prefix"],
util.T(hookConfig["table"]).String()),
extraItems,
hookOpts...,
)
l.AddHook(hook)
return hook
default:
}
}
return nil
}