-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
server.go
426 lines (369 loc) · 12 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
package goyave
import (
"context"
"database/sql"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"sync/atomic"
"syscall"
"time"
"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"
)
// 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
startupHooks []func(*Server)
shutdownHooks []func(*Server)
state uint32 // 0 -> created, 1 -> preparing, 2 -> ready, 3 -> stopped
}
// New create a new `Server` using automatically loaded configuration.
// See `config.Load()` for more details.
func New() (*Server, error) {
cfg, err := config.Load()
if err != nil {
return nil, errors.New(err)
}
return NewWithConfig(cfg)
}
// NewWithConfig create a new `Server` using the provided configuration.
func NewWithConfig(cfg *config.Config) (*Server, error) { // TODO with options? (for loggers, lang, etc) Could take a io.FS as input for resources directory
// TODO explicitly return *errors.Error?
slogger := slog.New(slog.NewHandler(cfg.GetBool("app.debug"), os.Stderr))
languages := lang.New() // TODO using embed FS
languages.Default = cfg.GetString("app.defaultLanguage")
if err := languages.LoadAllAvailableLanguages(); err != nil {
return nil, err
}
host := cfg.GetString("server.host") + ":" + strconv.Itoa(cfg.GetInt("server.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,
IdleTimeout: time.Duration(cfg.GetInt("server.idleTimeout")) * time.Second,
},
config: cfg,
services: make(map[string]Service),
Lang: languages,
stopChannel: make(chan struct{}, 1),
startupHooks: []func(*Server){},
shutdownHooks: []func(*Server){},
host: host,
baseURL: getAddress(cfg),
proxyBaseURL: getProxyAddress(cfg),
Logger: slogger,
}
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 getAddress(cfg *config.Config) string {
port := cfg.GetInt("server.port")
shouldShowPort := 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(port)
}
return "http://" + host
}
func getProxyAddress(cfg *config.Config) string {
if !cfg.Has("server.proxy.host") {
return 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")
}
// 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.New(fmt.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
}
// 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 {
state := atomic.LoadUint32(&s.state)
return state == 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 {
return errors.New(err)
}
err = db.Close()
if err != nil {
err = errors.New(err)
}
return err
}
// 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.
//
// The `routeRegistrer` parameter is a function aimed at registering all your routes and middleware.
//
// Errors returned can be safely type-asserted to `*goyave.Error`.
func (s *Server) Start() error {
state := atomic.LoadUint32(&s.state)
if state == 1 || state == 2 {
return errors.New("server is already running")
} else if state == 3 {
return errors.New("cannot restart a stopped server")
}
atomic.StoreUint32(&s.state, 1)
defer func() {
atomic.StoreUint32(&s.state, 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)
}
defer func() {
for _, hook := range s.shutdownHooks {
hook(s)
}
if err := s.CloseDB(); err != nil {
s.Logger.Error(err)
}
}()
atomic.StoreUint32(&s.state, 2)
go func(s *Server) { // TODO document startup hooks share the same goroutine
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 && err != http.ErrServerClosed {
atomic.StoreUint32(&s.state, 3)
return errors.New(err)
}
return nil
}
// RegisterRoutes creates a new Router for this Server and runs the given `routeRegistrer`.
//
// This method is primarily used in tests so routes can be registered without starting the server.
// Starting the server will overwrite the previously registered routes.
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.
//
// 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.
func (s *Server) Stop() {
if s.sigChannel != nil {
signal.Stop(s.sigChannel)
close(s.sigChannel)
}
state := atomic.LoadUint32(&s.state)
atomic.StoreUint32(&s.state, 3)
if state == 0 {
// Start has not been called, do nothing
return
}
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
}