-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
server.go
536 lines (462 loc) · 15.6 KB
/
server.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package goyave
import (
"context"
"database/sql"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"sync/atomic"
"syscall"
"time"
stderrors "errors"
"gorm.io/gorm"
"goyave.dev/goyave/v5/config"
"goyave.dev/goyave/v5/database"
"goyave.dev/goyave/v5/lang"
"goyave.dev/goyave/v5/slog"
"goyave.dev/goyave/v5/util/errors"
"goyave.dev/goyave/v5/util/fsutil"
"goyave.dev/goyave/v5/util/fsutil/osfs"
)
// serverKey is a context key used to store the server instance into its base context.
type serverKey struct{}
// Options represent server creation options.
type Options struct {
// Config used by the server and propagated to all its components.
// If no configuration is provided, automatically load
// the default configuration using `config.Load()`.
Config *config.Config
// Logger used by the server and propagated to all its components.
// If no logger is provided in the options, uses the default logger.
Logger *slog.Logger
// LangFS the file system from which the language files
// will be loaded. This file system is expected to contain
// a `resources/lang` directory.
// If not provided, uses `osfs.FS` as a default.
LangFS fsutil.FS
// ConnState specifies an optional callback function that is
// called when a client connection changes state. See the
// `http.ConnState` type and associated constants for details.
ConnState func(net.Conn, http.ConnState)
// Context optionnally defines a function that returns the base context
// for the server. It will be used as base context for all incoming requests.
//
// The provided `net.Listener` is the specific Listener that's
// about to start accepting requests.
//
// If not given, the default is `context.Background()`.
//
// The context returned then has a the server instance added to it as a value.
// The server can thus be retrieved using `goyave.ServerFromContext(ctx)`.
//
// If the context is canceled, the server won't shut down automatically, you are
// responsible of calling `server.Stop()` if you want this to happen. Otherwise the
// server will continue serving requests, at the risk of generating "context canceled" errors.
BaseContext func(net.Listener) context.Context
// ConnContext optionally specifies a function that modifies
// the context used for a new connection `c`. The provided context
// is derived from the base context and has the server instance value, which can
// be retrieved using `goyave.ServerFromContext(ctx)`.
ConnContext func(ctx context.Context, c net.Conn) context.Context
// MaxHeaderBytes controls the maximum number of bytes the
// server will read parsing the request header's keys and
// values, including the request line. It does not limit the
// size of the request body.
// If zero, http.DefaultMaxHeaderBytes is used.
MaxHeaderBytes int
}
// Server the central component of a Goyave application.
type Server struct {
server *http.Server
config *config.Config
Lang *lang.Languages
router *Router
db *gorm.DB
services map[string]Service
// Logger the logger for default output
// Writes to stderr by default.
Logger *slog.Logger
host string
baseURL string
proxyBaseURL string
stopChannel chan struct{}
sigChannel chan os.Signal
ctx context.Context
baseContext func(net.Listener) context.Context
startupHooks []func(*Server)
shutdownHooks []func(*Server)
port int
state atomic.Uint32 // 0 -> created, 1 -> preparing, 2 -> ready, 3 -> stopped
}
// New create a new `Server` using the given options.
func New(opts Options) (*Server, error) {
cfg := opts.Config
if opts.Config == nil {
var err error
cfg, err = config.Load()
if err != nil {
return nil, errors.New(err)
}
}
slogger := opts.Logger
if slogger == nil {
slogger = slog.New(slog.NewHandler(cfg.GetBool("app.debug"), os.Stderr))
}
langFS := opts.LangFS
if langFS == nil {
langFS = &osfs.FS{}
}
languages := lang.New()
languages.Default = cfg.GetString("app.defaultLanguage")
if err := languages.LoadAllAvailableLanguages(langFS); err != nil {
return nil, err
}
port := cfg.GetInt("server.port")
host := cfg.GetString("server.host") + ":" + strconv.Itoa(port)
server := &Server{
server: &http.Server{
Addr: host,
WriteTimeout: time.Duration(cfg.GetInt("server.writeTimeout")) * time.Second,
ReadTimeout: time.Duration(cfg.GetInt("server.readTimeout")) * time.Second,
ReadHeaderTimeout: time.Duration(cfg.GetInt("server.readHeaderTimeout")) * time.Second,
IdleTimeout: time.Duration(cfg.GetInt("server.idleTimeout")) * time.Second,
ConnState: opts.ConnState,
ConnContext: opts.ConnContext,
MaxHeaderBytes: opts.MaxHeaderBytes,
},
ctx: context.Background(),
baseContext: opts.BaseContext,
config: cfg,
services: make(map[string]Service),
Lang: languages,
stopChannel: make(chan struct{}, 1),
startupHooks: []func(*Server){},
shutdownHooks: []func(*Server){},
host: cfg.GetString("server.host"),
port: port,
Logger: slogger,
}
server.server.BaseContext = server.internalBaseContext
server.refreshURLs()
server.server.ErrorLog = log.New(&errLogWriter{server: server}, "", 0)
if cfg.GetString("database.connection") != "none" {
db, err := database.New(cfg, func() *slog.Logger { return server.Logger })
if err != nil {
return nil, errors.New(err)
}
server.db = db
}
server.router = NewRouter(server)
server.server.Handler = server.router
return server, nil
}
func (s *Server) internalBaseContext(_ net.Listener) context.Context {
return s.ctx
}
func (s *Server) getAddress(cfg *config.Config) string {
shouldShowPort := s.port != 80
host := cfg.GetString("server.domain")
if len(host) == 0 {
host = cfg.GetString("server.host")
if host == "0.0.0.0" {
host = "127.0.0.1"
}
}
if shouldShowPort {
host += ":" + strconv.Itoa(s.port)
}
return "http://" + host
}
func (s *Server) getProxyAddress(cfg *config.Config) string {
if !cfg.Has("server.proxy.host") {
return s.getAddress(cfg)
}
var shouldShowPort bool
proto := cfg.GetString("server.proxy.protocol")
port := cfg.GetInt("server.proxy.port")
if proto == "https" {
shouldShowPort = port != 443
} else {
shouldShowPort = port != 80
}
host := cfg.GetString("server.proxy.host")
if shouldShowPort {
host += ":" + strconv.Itoa(port)
}
return proto + "://" + host + cfg.GetString("server.proxy.base")
}
func (s *Server) refreshURLs() {
s.baseURL = s.getAddress(s.config)
s.proxyBaseURL = s.getProxyAddress(s.config)
}
// Service returns the service identified by the given name.
// Panics if no service could be found with the given name.
func (s *Server) Service(name string) Service {
if s, ok := s.services[name]; ok {
return s
}
panic(errors.Errorf("service %q does not exist", name))
}
// LookupService search for a service by its name. If the service
// identified by the given name exists, it is returned with the `true` boolean.
// Otherwise returns `nil` and `false`.
func (s *Server) LookupService(name string) (Service, bool) {
service, ok := s.services[name]
return service, ok
}
// RegisterService on thise server using its name (returned by `Service.Name()`).
// A service's name should be unique.
// `Service.Init(server)` is called on the given service upon registration.
func (s *Server) RegisterService(service Service) {
s.services[service.Name()] = service
}
// Host returns the hostname and port the server is running on.
func (s *Server) Host() string {
return s.host + ":" + strconv.Itoa(s.port)
}
// Port returns the port the server is running on.
func (s *Server) Port() int {
return s.port
}
// BaseURL returns the base URL of your application.
// If "server.domain" is set in the config, uses it instead
// of an IP address.
func (s *Server) BaseURL() string {
return s.baseURL
}
// ProxyBaseURL returns the base URL of your application based on the "server.proxy" configuration.
// This is useful when you want to generate an URL when your application is served behind a reverse proxy.
// If "server.proxy.host" configuration is not set, returns the same value as "BaseURL()".
func (s *Server) ProxyBaseURL() string {
return s.proxyBaseURL
}
// IsReady returns true if the server has finished initializing and
// is ready to serve incoming requests.
// This operation is concurrently safe.
func (s *Server) IsReady() bool {
return s.state.Load() == 2
}
// RegisterStartupHook to execute some code once the server is ready and running.
// All startup hooks are executed in a single goroutine and in order of registration.
func (s *Server) RegisterStartupHook(hook func(*Server)) {
s.startupHooks = append(s.startupHooks, hook)
}
// ClearStartupHooks removes all startup hooks.
func (s *Server) ClearStartupHooks() {
s.startupHooks = []func(*Server){}
}
// RegisterShutdownHook to execute some code after the server stopped.
// Shutdown hooks are executed before `Start()` returns and are NOT executed
// in a goroutine, meaning that the shutdown process can be blocked by your
// shutdown hooks. It is your responsibility to implement a timeout mechanism
// inside your hook if necessary.
func (s *Server) RegisterShutdownHook(hook func(*Server)) {
s.shutdownHooks = append(s.shutdownHooks, hook)
}
// ClearShutdownHooks removes all shutdown hooks.
func (s *Server) ClearShutdownHooks() {
s.shutdownHooks = []func(*Server){}
}
// Config returns the server's config.
func (s *Server) Config() *config.Config {
return s.config
}
// DB returns the root database instance. Panics if no
// database connection is set up.
func (s *Server) DB() *gorm.DB {
if s.db == nil {
panic(errors.NewSkip("No database connection. Database is set to \"none\" in the config", 3))
}
return s.db
}
// Transaction makes it so all DB requests are run inside a transaction.
//
// Returns the rollback function. When you are done, call this function to
// complete the transaction and roll it back. This will also restore the original
// DB so it can be used again out of the transaction.
//
// This is used for tests. This operation is not concurrently safe.
func (s *Server) Transaction(opts ...*sql.TxOptions) func() {
if s.db == nil {
panic(errors.NewSkip("No database connection. Database is set to \"none\" in the config", 3))
}
ogDB := s.db
s.db = s.db.Begin(opts...)
return func() {
err := s.db.Rollback().Error
s.db = ogDB
if err != nil {
panic(errors.New(err))
}
}
}
// ReplaceDB manually replace the automatic DB connection.
// If a connection already exists, closes it before discarding it.
// This can be used to create a mock DB in tests. Using this function
// is not recommended outside of tests. Prefer using a custom dialect.
// This operation is not concurrently safe.
func (s *Server) ReplaceDB(dialector gorm.Dialector) error {
if err := s.CloseDB(); err != nil {
return err
}
db, err := database.NewFromDialector(s.config, func() *slog.Logger { return s.Logger }, dialector)
if err != nil {
return err
}
s.db = db
return nil
}
// CloseDB close the database connection if there is one.
// Does nothing and returns `nil` if there is no connection.
func (s *Server) CloseDB() error {
if s.db == nil {
return nil
}
db, err := s.db.DB()
if err != nil {
if stderrors.Is(err, gorm.ErrInvalidDB) {
return nil
}
return errors.New(err)
}
return errors.New(db.Close())
}
// Router returns the root router.
func (s *Server) Router() *Router {
return s.router
}
// Start the server. This operation is blocking and returns when the server is closed.
func (s *Server) Start() error {
swapped := s.state.CompareAndSwap(0, 1)
if !swapped {
return errors.New("server was already started")
}
defer func() {
s.state.Store(3)
// Notify the shutdown is complete so Stop() can return
s.stopChannel <- struct{}{}
close(s.stopChannel)
}()
ln, err := net.Listen("tcp", s.server.Addr)
if err != nil {
return errors.New(err)
}
baseCtx := context.Background()
if s.baseContext != nil {
baseCtx = s.baseContext(ln)
if baseCtx == nil {
panic("server options BaseContext returned a nil context")
}
}
s.ctx = context.WithValue(baseCtx, serverKey{}, s)
select {
case <-s.ctx.Done():
return errors.New("cannot start the server, context is canceled")
default:
}
s.port = ln.Addr().(*net.TCPAddr).Port
s.refreshURLs()
defer func() {
for _, hook := range s.shutdownHooks {
hook(s)
}
if err := s.CloseDB(); err != nil {
s.Logger.Error(err)
}
}()
s.state.Store(2)
go func(s *Server) {
if s.IsReady() {
// We check if the server is ready to prevent startup hook execution
// if `Serve` returned an error before the goroutine started
for _, hook := range s.startupHooks {
hook(s)
}
}
}(s)
if err := s.server.Serve(ln); err != nil && !stderrors.Is(err, http.ErrServerClosed) {
s.state.Store(3)
return errors.New(err)
}
return nil
}
// RegisterRoutes runs the given `routeRegistrer` function with this Server and its router.
// The router's regex cache is cleared after the `routeRegistrer` function returns.
// This method should only be called once.
func (s *Server) RegisterRoutes(routeRegistrer func(*Server, *Router)) {
routeRegistrer(s, s.router)
s.router.ClearRegexCache()
}
// Stop gracefully shuts down the server without interrupting any
// active connections.
//
// `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. This can be done using shutdown hooks.
//
// If registered, the OS signal channel is closed.
//
// Make sure the program doesn't exit before `Stop()` returns.
//
// After being stopped, a `Server` is not meant to be re-used.
//
// This function can be called from any goroutine and is concurrently safe.
// Calling this function several times is safe. Calls after the first one are no-op.
func (s *Server) Stop() {
state := s.state.Swap(3)
if state == 0 || state == 3 {
// Start has not been called or Stop has already been called, do nothing
return
}
if s.sigChannel != nil {
signal.Stop(s.sigChannel)
close(s.sigChannel)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := s.server.Shutdown(ctx)
if err != nil {
s.Logger.Error(errors.NewSkip(err, 3))
}
<-s.stopChannel // Wait for stop channel before returning
}
// RegisterSignalHook creates a channel listening on SIGINT and SIGTERM. When receiving such
// signal, the server is stopped automatically and the listener on these signals is removed.
func (s *Server) RegisterSignalHook() {
// Sometimes users may not want to have a sigChannel setup
// also we don't want it in tests
// users will have to manually call this function if they want the shutdown on signal feature
s.sigChannel = make(chan os.Signal, 64)
signal.Notify(s.sigChannel, syscall.SIGINT, syscall.SIGTERM)
go func() {
_, ok := <-s.sigChannel
if ok {
s.Stop()
}
}()
}
// errLogWriter is a proxy io.Writer that pipes into the server logger.
// This is used so the error logger (type `*log.Logger`) of the underlying
// std HTTP server write to the same logger as the rest of the application.
type errLogWriter struct {
server *Server
}
func (w errLogWriter) Write(p []byte) (n int, err error) {
w.server.Logger.Error(fmt.Errorf("%s", p))
return len(p), nil
}
// ServerFromContext returns the `*goyave.Server` stored in the given context or `nil`.
// This is safe to call using any context retrieved from incoming HTTP requests as this value
// is automatically injected when the server is created.
func ServerFromContext(ctx context.Context) *Server {
s, ok := ctx.Value(serverKey{}).(*Server)
if !ok {
return nil
}
return s
}