forked from go-goyave/goyave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goyave.go
428 lines (373 loc) Β· 10.2 KB
/
goyave.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package goyave
import (
"context"
"log"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
"goyave.dev/goyave/v3/config"
"goyave.dev/goyave/v3/database"
"goyave.dev/goyave/v3/lang"
)
var (
server *http.Server
redirectServer *http.Server
router *Router
maintenanceHandler http.Handler
sigChannel chan os.Signal
tlsStopChannel chan struct{} = make(chan struct{}, 1)
stopChannel chan struct{} = make(chan struct{}, 1)
hookChannel chan struct{} = make(chan struct{}, 1)
// Critical config entries (cached for better performance)
protocol string
maxPayloadSize int64
defaultLanguage string
startupHooks []func()
shutdownHooks []func()
ready bool = false
maintenanceEnabled bool = false
mutex = &sync.RWMutex{}
once sync.Once
// Logger the logger for default output
// Writes to stdout by default.
Logger *log.Logger = log.New(os.Stdout, "", log.LstdFlags)
// AccessLogger the logger for access. This logger
// is used by the logging middleware.
// Writes to stdout by default.
AccessLogger *log.Logger = log.New(os.Stdout, "", 0)
// ErrLogger the logger in which errors and stacktraces are written.
// Writes to stderr by default.
ErrLogger *log.Logger = log.New(os.Stderr, "", log.LstdFlags)
)
const (
// ExitInvalidConfig the exit code returned when the config
// validation doesn't pass.
ExitInvalidConfig = 3
// ExitNetworkError the exit code returned when an error
// occurs when opening the network listener
ExitNetworkError = 4
// ExitHTTPError the exit code returned when an error
// occurs in the HTTP server (port already in use for example)
ExitHTTPError = 5
)
// Error wrapper for errors directely related to the server itself.
// Contains an exit code and the original error.
type Error struct {
Err error
ExitCode int
}
func (e *Error) Error() string {
return e.Err.Error()
}
// IsReady returns true if the server has finished initializing and
// is ready to serve incoming requests.
func IsReady() bool {
mutex.RLock()
defer mutex.RUnlock()
return ready
}
// RegisterStartupHook to execute some code once the server is ready and running.
func RegisterStartupHook(hook func()) {
mutex.Lock()
startupHooks = append(startupHooks, hook)
mutex.Unlock()
}
// ClearStartupHooks removes all startup hooks.
func ClearStartupHooks() {
mutex.Lock()
startupHooks = []func(){}
mutex.Unlock()
}
// RegisterShutdownHook to execute some code after the server stopped.
// Shutdown hooks are executed before goyave.Start() returns.
func RegisterShutdownHook(hook func()) {
mutex.Lock()
shutdownHooks = append(shutdownHooks, hook)
mutex.Unlock()
}
// ClearShutdownHooks removes all shutdown hooks.
func ClearShutdownHooks() {
mutex.Lock()
shutdownHooks = []func(){}
mutex.Unlock()
}
// Start starts the web server.
// The routeRegistrer parameter is a function aimed at registering all your routes and middleware.
// import (
// "goyave.dev/goyave/v3"
// "github.com/username/projectname/route"
// )
//
// func main() {
// if err := goyave.Start(route.Register); err != nil {
// os.Exit(err.(*goyave.Error).ExitCode)
// }
// }
//
// Errors returned can be safely type-asserted to "*goyave.Error".
// Panics if the server is already running.
func Start(routeRegistrer func(*Router)) error {
if IsReady() {
ErrLogger.Panicf("Server is already running.")
}
mutex.Lock()
if !config.IsLoaded() {
if err := config.Load(); err != nil {
ErrLogger.Println(err)
mutex.Unlock()
return &Error{err, ExitInvalidConfig}
}
}
// Performance improvements by loading critical config entries beforehand
cacheCriticalConfig()
lang.LoadDefault()
lang.LoadAllAvailableLanguages()
if config.GetBool("database.autoMigrate") && config.GetString("database.connection") != "none" {
database.Migrate()
}
router = NewRouter()
routeRegistrer(router)
router.ClearRegexCache()
return startServer(router)
}
func cacheCriticalConfig() {
maxPayloadSize = int64(config.GetFloat("server.maxUploadSize") * 1024 * 1024)
defaultLanguage = config.GetString("app.defaultLanguage")
protocol = config.GetString("server.protocol")
}
// EnableMaintenance replace the main server handler with the "Service Unavailable" handler.
func EnableMaintenance() {
mutex.Lock()
server.Handler = getMaintenanceHandler()
maintenanceEnabled = true
mutex.Unlock()
}
// DisableMaintenance replace the main server handler with the original router.
func DisableMaintenance() {
mutex.Lock()
server.Handler = router
maintenanceEnabled = false
mutex.Unlock()
}
// IsMaintenanceEnabled return true if the server is currently in maintenance mode.
func IsMaintenanceEnabled() bool {
mutex.RLock()
defer mutex.RUnlock()
return maintenanceEnabled
}
// GetRoute get a named route.
// Returns nil if the route doesn't exist.
func GetRoute(name string) *Route {
mutex.Lock()
defer mutex.Unlock()
return router.namedRoutes[name]
}
func getMaintenanceHandler() http.Handler {
once.Do(func() {
maintenanceHandler = http.HandlerFunc(func(resp http.ResponseWriter, request *http.Request) {
resp.WriteHeader(http.StatusServiceUnavailable)
})
})
return maintenanceHandler
}
// Stop gracefully shuts down the server without interrupting any
// active connections.
//
// Make sure the program doesn't exit and waits instead for Stop to return.
//
// Stop does not attempt to close nor wait for hijacked
// connections such as WebSockets. The caller of Stop should
// separately notify such long-lived connections of shutdown and wait
// for them to close, if desired.
func Stop() {
mutex.Lock()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
stop(ctx)
if sigChannel != nil {
hookChannel <- struct{}{} // Clear shutdown hook
<-hookChannel
sigChannel = nil
}
mutex.Unlock()
}
func stop(ctx context.Context) error {
var err error
if server != nil {
err = server.Shutdown(ctx)
database.Close()
server = nil
router = nil
ready = false
maintenanceEnabled = false
if redirectServer != nil {
redirectServer.Shutdown(ctx)
<-tlsStopChannel
redirectServer = nil
}
for _, hook := range shutdownHooks {
hook()
}
stopChannel <- struct{}{}
}
return err
}
func getHost(protocol string) string {
var port string
if protocol == "https" {
port = "server.httpsPort"
} else {
port = "server.port"
}
return config.GetString("server.host") + ":" + strconv.Itoa(config.GetInt(port))
}
func getAddress(protocol string) string {
var shouldShowPort bool
var port string
if protocol == "https" {
p := config.GetInt("server.httpsPort")
port = strconv.Itoa(p)
shouldShowPort = p != 443
} else {
p := config.GetInt("server.port")
port = strconv.Itoa(p)
shouldShowPort = p != 80
}
host := config.GetString("server.domain")
if len(host) == 0 {
host = config.GetString("server.host")
if host == "0.0.0.0" {
host = "127.0.0.1"
}
}
if shouldShowPort {
host += ":" + port
}
return protocol + "://" + host
}
// BaseURL returns the base URL of your application.
func BaseURL() string {
return getAddress(config.GetString("server.protocol"))
}
func startTLSRedirectServer() {
httpsAddress := getAddress("https")
timeout := time.Duration(config.GetInt("server.timeout")) * time.Second
redirectServer = &http.Server{
Addr: getHost("http"),
WriteTimeout: timeout,
ReadTimeout: timeout,
IdleTimeout: timeout * 2,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
address := httpsAddress + r.URL.Path
query := r.URL.Query()
if len(query) != 0 {
address += "?" + query.Encode()
}
http.Redirect(w, r, address, http.StatusPermanentRedirect)
}),
}
ln, err := net.Listen("tcp", redirectServer.Addr)
if err != nil {
ErrLogger.Printf("The TLS redirect server encountered an error: %s\n", err.Error())
redirectServer = nil
return
}
ok := ready
r := redirectServer
go func() {
if ok && r != nil {
if err := r.Serve(ln); err != nil && err != http.ErrServerClosed {
ErrLogger.Printf("The TLS redirect server encountered an error: %s\n", err.Error())
mutex.Lock()
redirectServer = nil
ln.Close()
mutex.Unlock()
return
}
}
ln.Close()
tlsStopChannel <- struct{}{}
}()
}
func startServer(router *Router) error {
defer func() {
<-stopChannel // Wait for stop() to finish before returning
}()
timeout := time.Duration(config.GetInt("server.timeout")) * time.Second
server = &http.Server{
Addr: getHost(protocol),
WriteTimeout: timeout,
ReadTimeout: timeout,
IdleTimeout: timeout * 2,
Handler: router,
}
if config.GetBool("server.maintenance") {
server.Handler = getMaintenanceHandler()
maintenanceEnabled = true
}
ln, err := net.Listen("tcp", server.Addr)
if err != nil {
ErrLogger.Println(err)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
stop(ctx)
mutex.Unlock()
return &Error{err, ExitNetworkError}
}
defer ln.Close()
readyChan := make(chan struct{})
registerShutdownHook(readyChan, stop)
<-readyChan
close(readyChan)
ready = true
if protocol == "https" {
startTLSRedirectServer()
s := server
mutex.Unlock()
runStartupHooks()
if err := s.ServeTLS(ln, config.GetString("server.tls.cert"), config.GetString("server.tls.key")); err != nil && err != http.ErrServerClosed {
ErrLogger.Println(err)
Stop()
return &Error{err, ExitHTTPError}
}
} else {
s := server
mutex.Unlock()
runStartupHooks()
if err := s.Serve(ln); err != nil && err != http.ErrServerClosed {
ErrLogger.Println(err)
Stop()
return &Error{err, ExitHTTPError}
}
}
return nil
}
func runStartupHooks() {
for _, hook := range startupHooks {
go hook()
}
}
func registerShutdownHook(readyChan chan struct{}, hook func(context.Context) error) {
sigChannel = make(chan os.Signal, 64)
signal.Notify(sigChannel, syscall.SIGINT, syscall.SIGTERM)
go func() {
readyChan <- struct{}{}
select {
case <-hookChannel:
hookChannel <- struct{}{}
case <-sigChannel: // Block until SIGINT or SIGTERM received
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
mutex.Lock()
sigChannel = nil
hook(ctx)
mutex.Unlock()
}
}()
}
// TODO refactor server sartup (use context)