-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
226 lines (195 loc) · 5.3 KB
/
app.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package app
import (
"crypto/tls"
"errors"
"github.com/go-chi/chi"
"github.com/sirupsen/logrus"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/sync/errgroup"
"github.com/moocss/chi-webserver/src/config"
"github.com/moocss/chi-webserver/src/dao"
"github.com/moocss/chi-webserver/src/pkg/log"
"github.com/moocss/chi-webserver/src/router"
"github.com/moocss/chi-webserver/src/service"
"github.com/moocss/chi-webserver/src/util"
)
// App 项目
type App struct {
config *config.Config
dao *dao.Dao
service service.Service
}
// New 实例化App
func New(cfg *config.Config, dao *dao.Dao, svc service.Service) *App {
return &App{
config: cfg,
dao: dao,
service: svc,
}
}
// InitLog 初始化日志配置
func (app *App) InitLog() {
if app.config.Log.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
if app.config.Log.Trace {
logrus.SetLevel(logrus.TraceLevel)
}
if app.config.Log.Text {
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: app.config.Log.Color,
DisableColors: !app.config.Log.Color,
})
} else {
logrus.SetFormatter(&logrus.JSONFormatter{
PrettyPrint: app.config.Log.Pretty,
})
}
}
// RunHTTPServer provide run http or https protocol.
func (app *App) RunHTTPServer() (err error) {
if !app.config.Core.Enabled {
log.Debug("httpd app is disabled.")
return nil
}
if app.config.Core.AutoTLS.Enabled {
return app.autoTLSServer()
} else if app.config.Core.TLS.CertPath != "" && app.config.Core.TLS.KeyPath != "" {
return app.defaultTLSServer()
} else {
return app.defaultServer()
}
}
func (app *App) autoTLSServer() error {
var g errgroup.Group
dir := util.CacheDir()
_ = os.MkdirAll(dir, 0700)
manager := &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(app.config.Core.AutoTLS.Host),
// Cache: autocert.DirCache(app.config.Core.AutoTLS.Folder),
Cache: autocert.DirCache(dir),
}
g.Go(func() error {
return http.ListenAndServe(":http", manager.HTTPHandler(http.HandlerFunc(app.redirect)))
})
g.Go(func() error {
serve := &http.Server{
Addr: ":https",
TLSConfig: &tls.Config{
GetCertificate: manager.GetCertificate,
NextProtos: []string{"http/1.1"}, // disable h2 because Safari :(
},
Handler: serve(app),
}
app.handleSignal(serve)
log.Info("Start to listening the incoming requests on https address")
return serve.ListenAndServeTLS("", "")
})
return g.Wait()
}
func (app *App) defaultTLSServer() error {
var g errgroup.Group
g.Go(func() error {
return http.ListenAndServe(":http", http.HandlerFunc(app.redirect))
})
g.Go(func() error {
serve := &http.Server{
Addr: ":https",
Handler: serve(app),
TLSConfig: &tls.Config{
NextProtos: []string{"http/1.1"}, // disable h2 because Safari :(
},
}
app.handleSignal(serve)
log.Infof("Start to listening the incoming requests on https address: %s", app.config.Core.TLS.Port)
return serve.ListenAndServeTLS(
app.config.Core.TLS.CertPath,
app.config.Core.TLS.KeyPath,
)
})
return g.Wait()
}
func (app *App) defaultServer() error {
serve := &http.Server{
Addr: "0.0.0.0:" + app.config.Core.Port,
Handler: serve(app),
}
app.handleSignal(serve)
log.Infof("Start to listening the incoming requests on http address: %s", app.config.Core.Port)
return serve.ListenAndServe()
}
// redirect ...
func (app *App) redirect(w http.ResponseWriter, req *http.Request) {
var serverHost = app.config.Core.Host
serverHost = strings.TrimPrefix(serverHost, "http://")
serverHost = strings.TrimPrefix(serverHost, "https://")
req.URL.Scheme = "https"
req.URL.Host = serverHost
w.Header().Set("Strict-Transport-Security", "max-age=31536000")
http.Redirect(w, req, req.URL.String(), http.StatusMovedPermanently)
}
// serve returns a app instance
func serve(app *App) *chi.Mux {
// Set gin mode.
setRuntimeMode(app.config.Core.Mode)
// Setup the app
handler := router.Load(
// Services
app.service,
// Middlwares
)
return handler
}
// setRuntimeMode 设置开发模式
func setRuntimeMode(mode string) {
switch mode {
case "dev":
gin.SetMode(gin.DebugMode)
case "prod":
gin.SetMode(gin.ReleaseMode)
case "test":
gin.SetMode(gin.TestMode)
default:
panic("unknown mode")
}
}
// handleSignal handles system signal for graceful shutdown.
func (app *App) handleSignal(server *http.Server) {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
s := <-c
log.Infof("got signal [%s], exiting apiserver now", s)
if err := server.Close(); nil != err {
log.Errorf("server close failed: %s ", err)
}
// 退出服务时,关闭数据库
app.dao.DB.Close()
log.Info("WebServer exited")
os.Exit(0)
}()
}
// PingServer 服务心跳检查
func (app *App) PingServer() (err error) {
maxPingConf := app.config.Core.MaxPingCount
for i := 0; i < maxPingConf; i++ {
// Ping the app by sending a GET request to `/health`.
resp, err := http.Get("http://localhost:" + app.config.Core.Port + "/sd/health")
if err == nil && resp.StatusCode == 200 {
return nil
}
// Sleep for a second to continue the next ping.
log.Info("Waiting for the router, retry in 1 second.")
time.Sleep(time.Second)
}
err = errors.New("Cannot connect to the router.")
return
}