-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
172 lines (147 loc) · 4.57 KB
/
main.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
172
package main
import (
"context"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/jassue/gin-wire/app/command"
"github.com/jassue/gin-wire/config"
"github.com/jassue/gin-wire/util/path"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
)
var (
rootPath = path.RootPath()
Version string
configPath string
conf *config.Configuration
loggerWriter *lumberjack.Logger
logger *zap.Logger
)
func init() {
pflag.StringVarP(&configPath, "conf", "", filepath.Join(rootPath, "conf", "config.yaml"), "config path, eg: --conf config.yaml")
cobra.OnInitialize(func() {
initConfig()
initLogger()
})
}
func main() {
rootCmd := &cobra.Command{
Use: "app",
Run: func(cmd *cobra.Command, args []string) {
app, cleanup, err := wireApp(conf, loggerWriter, logger)
if err != nil {
panic(err)
}
defer cleanup()
// 启动应用
log.Printf("start app %s ...", Version)
if err := app.Run(); err != nil {
panic(err)
}
// 等待中断信号以优雅地关闭应用
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Printf("shutdown app %s ...", Version)
// 设置 5 秒的超时时间
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// 关闭应用
if err := app.Stop(ctx); err != nil {
panic(err)
}
},
}
// 注册命令
command.Register(rootCmd, func() (*command.Command, func(), error) {
return wireCommand(conf, loggerWriter, logger)
})
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}
func initConfig() {
if !filepath.IsAbs(configPath) {
configPath = filepath.Join(rootPath, "conf", configPath)
}
fmt.Println("load config:" + configPath)
v := viper.New()
v.SetConfigFile(configPath)
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("read config failed: %s \n", err))
}
if err := v.Unmarshal(&conf); err != nil {
fmt.Println(err)
}
v.WatchConfig()
v.OnConfigChange(func(in fsnotify.Event) {
fmt.Println("config file changed:", in.Name)
defer func() {
if err := recover(); err != nil {
logger.Error("config file changed err:", zap.Any("err", err))
fmt.Println(err)
}
}()
if err := v.Unmarshal(&conf); err != nil {
fmt.Println(err)
}
})
}
func initLogger() {
var level zapcore.Level // zap 日志等级
var options []zap.Option // zap 配置项
logFileDir := conf.Log.RootDir
if !filepath.IsAbs(logFileDir) {
logFileDir = filepath.Join(rootPath, logFileDir)
}
if ok, _ := path.Exists(logFileDir); !ok {
_ = os.Mkdir(conf.Log.RootDir, os.ModePerm)
}
switch conf.Log.Level {
case "debug":
level = zap.DebugLevel
options = append(options, zap.AddStacktrace(level))
case "info":
level = zap.InfoLevel
case "warn":
level = zap.WarnLevel
case "error":
level = zap.ErrorLevel
options = append(options, zap.AddStacktrace(level))
case "dpanic":
level = zap.DPanicLevel
case "panic":
level = zap.PanicLevel
case "fatal":
level = zap.FatalLevel
default:
level = zap.InfoLevel
}
// 调整编码器默认配置
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = func(time time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(time.Format("2006-01-02 15:04:05.000"))
}
encoderConfig.EncodeLevel = func(l zapcore.Level, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(conf.App.Env + "." + l.String())
}
loggerWriter = &lumberjack.Logger{
Filename: filepath.Join(logFileDir, conf.Log.Filename),
MaxSize: conf.Log.MaxSize,
MaxBackups: conf.Log.MaxBackups,
MaxAge: conf.Log.MaxAge,
Compress: conf.Log.Compress,
}
logger = zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(encoderConfig), zapcore.AddSync(loggerWriter), level), options...)
}