-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
193 lines (171 loc) · 4.34 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/camtrik/gin-blog/global"
"github.com/camtrik/gin-blog/internal/model"
"github.com/camtrik/gin-blog/internal/routers"
"github.com/camtrik/gin-blog/pkg/logger"
"github.com/camtrik/gin-blog/pkg/setting"
"github.com/camtrik/gin-blog/pkg/tracer"
"github.com/gin-gonic/gin"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
port string
runMode string
config string
isVersion bool
buildTime string
buildVersion string
gitCommitId string
)
func init() {
err := setupFlag()
if err != nil {
log.Fatalf("init.setupFlag err: %v", err)
}
err = setupSetting()
if err != nil {
log.Fatalf("init.setupSetting err: %v", err)
}
err = setupDBEngine()
if err != nil {
log.Fatalf("init.setupDBEngine err: %v", err)
}
err = setupLogger()
if err != nil {
log.Fatalf("init.setupLogger err: %v", err)
}
err = setupTracer()
if err != nil {
log.Fatalf("init.setupTracer err: %v", err)
}
}
// @title gin-blog
// @version 1.0
// @description A blog system backend framework based on Gin.
// @termsOfService https://github.com/camtrik/gin-blog
func main() {
// log.Printf("global.ServerSetting: %+v\n", global.ServerSetting)
// log.Printf("global.AppSetting: %+v\n", global.AppSetting)
// log.Printf("global.DatabaseSetting: %+v\n", global.DatabaseSetting)
// global.Logger.Infof(c, "%s: www/%s", "ebbi", "gin-blog")
if isVersion {
fmt.Printf("build time: %s\n", buildTime)
fmt.Printf("build version: %s\n", buildVersion)
fmt.Printf("git commit id: %s\n", gitCommitId)
return
}
gin.SetMode(global.ServerSetting.RunMode)
router := routers.NewRouter()
s := &http.Server{
Addr: ":" + strconv.Itoa(global.ServerSetting.HttpPort),
Handler: router,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 20,
}
// restart server with a new goroutine
go func() {
err := s.ListenAndServe()
if err != nil && err != http.ErrAbortHandler {
log.Fatal("s.ListenAndServe err: %v", err)
}
}()
// wait for interrupt signal
quit := make(chan os.Signal)
// receive syscall.SIGINT, syscall.SIGTERM
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
// shutdown server with a 5-second timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown:", err)
}
log.Println("Server exiting")
}
func setupSetting() error {
// "," to split multiple config files
setting, err := setting.NewSetting(strings.Split(config, ",")...)
if err != nil {
return err
}
err = setting.ReadSection("Server", &global.ServerSetting)
if err != nil {
return err
}
err = setting.ReadSection("App", &global.AppSetting)
if err != nil {
return err
}
err = setting.ReadSection("Database", &global.DatabaseSetting)
if err != nil {
return err
}
err = setting.ReadSection("JWT", &global.JWTSetting)
if err != nil {
return err
}
err = setting.ReadSection("Email", &global.EmailSetting)
if err != nil {
return err
}
if port != "" {
global.ServerSetting.HttpPort, _ = strconv.Atoi(port)
}
if runMode != "" {
global.ServerSetting.RunMode = runMode
}
global.ServerSetting.ReadTimeout *= time.Second
global.ServerSetting.WriteTimeout *= time.Second
global.JWTSetting.Expire *= time.Second
return nil
}
func setupDBEngine() error {
var err error
global.DBEngine, err = model.NewDBEngine(global.DatabaseSetting)
if err != nil {
return err
}
return nil
}
func setupLogger() error {
global.Logger = logger.NewLogger(&lumberjack.Logger{
Filename: global.AppSetting.LogSavePath + "/" + global.AppSetting.LogFileName + global.AppSetting.LogFileExt,
MaxSize: 600,
MaxAge: 10,
LocalTime: true,
}, "", log.LstdFlags).WithCaller(2)
return nil
}
func setupTracer() error {
jaegerTracer, _, err := tracer.NewJaegerTracer(
"gin-blog",
"localhost:6831",
)
if err != nil {
return err
}
global.Tracer = jaegerTracer
return nil
}
func setupFlag() error {
flag.StringVar(&port, "port", "", "server port")
flag.StringVar(&runMode, "mode", "", "run mode")
flag.StringVar(&config, "config", "./configs/", "config path")
flag.BoolVar(&isVersion, "version", false, "show compile info")
flag.Parse()
return nil
}