forked from limexc/MuRi-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (61 loc) · 1.43 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
package main
import (
"MURI-GO/dao/mysql"
"MURI-GO/logger"
"MURI-GO/router"
"MURI-GO/settings"
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"go.uber.org/zap"
)
func main() {
//1、加载配置文件
if err := settings.Init(); err != nil {
fmt.Println("init settings failed")
return
}
//2、日志初始化
if err := logger.Init(); err != nil {
fmt.Println("init logger failed")
return
}
defer zap.L().Sync()
zap.L().Debug("logger init success .....")
//初始化mysql
if err := mysql.Init(); err != nil {
zap.L().Fatal("init mysql failed", zap.Error(err))
}
defer mysql.Close()
//注册路由
r := gin.Default()
router.Router.InitApiRouter(r)
//启动gin server
srv := &http.Server{
Addr: fmt.Sprintf(":%d", viper.GetInt("app.port")),
Handler: r,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
zap.L().Error("", zap.Error(err))
}
}()
//优雅关闭
//声明一个系统信号的channel,如果没有信号就一直阻塞,如果有就执行
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
//设置ctx超时时间
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
//关闭gin
if err := srv.Shutdown(ctx); err != nil {
zap.L().Fatal("gin server 关闭异常:", zap.Error(err))
}
zap.L().Info("gin server 退出成功")
}