-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (157 loc) · 4.11 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/okh8609/gin_blog/global"
"github.com/okh8609/gin_blog/internal/model"
"github.com/okh8609/gin_blog/internal/routers"
"github.com/okh8609/gin_blog/pkg/logger"
"github.com/okh8609/gin_blog/pkg/setting"
"github.com/okh8609/gin_blog/pkg/tracer"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
// go build -ldflags "-X main.buildTime=`date +%Y-%m-%d,%H:%M:%S` -X main.buildVersion=1.0.0 -X main.getCommitID=`git rev-parse HEAD`"
showVersion bool
buildTime string
buildVersion string
getCommitID string
)
var httpPort string
var runMode string
var configPathes string
func init() {
setupFlag()
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 部落格後端系統
// @version 1.0
// @description 再強一點:用Go語言完成六個大型專案
// @termsOfService https://github.com/okh8609/gin_blog
// @contact.name Khaos_Ou
// @host localhost:8080
func main() {
if showVersion {
fmt.Printf("build_time: %s\n", buildTime)
fmt.Printf("build_version: %s\n", buildVersion)
fmt.Printf("git_commit_id: %s\n", getCommitID)
return
}
global.MyLogger.Infof(context.Background(), "%s: okh8609/%s", "Khaos", "gin_blog")
gin.SetMode(global.Server.RunMode)
engin := routers.NewRouter()
engin.HandleMethodNotAllowed = global.Server.HandleMethodNotAllowed
server := http.Server{
Addr: ":" + global.Server.HttpPort,
Handler: engin,
ReadTimeout: global.Server.ReadTimeout * time.Second,
WriteTimeout: global.Server.WriteTimeout * time.Second,
MaxHeaderBytes: 1 << 20,
}
// server.ListenAndServe()
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("server.ListenAndServe err: %v", err)
}
}()
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
<-c
log.Println("Shuting down server...")
cntx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(cntx); err != nil {
log.Fatal("Server forced to shutdown:", err)
}
log.Println("Server exiting")
}
func setupSetting() error {
ss, err := setting.NewSetting(strings.Split(configPathes, ",")...)
if err != nil {
return err
}
err = ss.ReadSection("Server", &global.Server)
if err != nil {
return err
}
err = ss.ReadSection("App", &global.App)
if err != nil {
return err
}
err = ss.ReadSection("Database", &global.Database)
if err != nil {
return err
}
err = ss.ReadSection("Auth", &global.Auth)
if err != nil {
return err
}
err = ss.ReadSection("Email", &global.Email)
if err != nil {
return err
}
if httpPort != "" {
global.Server.HttpPort = httpPort
}
if runMode != "" {
global.Server.RunMode = runMode
}
return nil
}
func setupDBEngine() error {
var err error
global.DBEngine, err = model.NewDBEngine(global.Database)
return err
}
func setupLogger() error {
path := global.App.LogSavePath + "/" + global.App.LogFileName + global.App.LogFileExt
global.MyLogger = logger.NewLogger(&lumberjack.Logger{
Filename: path,
MaxSize: 500, // MB
MaxAge: 28, // Days
MaxBackups: 3,
LocalTime: true,
Compress: false, // disabled by default
}, "", log.LstdFlags).WithCaller(2)
return nil
}
func setupTracer() error {
jaegerTracer, _, err := tracer.NewJaegerTracer("gin_blog", "127.0.0.1:6831")
if err != nil {
return err
}
global.Tracer = &jaegerTracer
return nil
}
func setupFlag() error {
flag.StringVar(&httpPort, "port", "8080", "啟動的通訊埠")
flag.StringVar(&runMode, "mode", "debug", "執行的模式 [debug | release]")
flag.StringVar(&configPathes, "cpath", "./configs/", "設定檔的尋找路徑")
flag.BoolVar(&showVersion, "version", false, "編譯資訊")
flag.Parse()
return nil
}