-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.go
72 lines (60 loc) · 1.98 KB
/
default.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
package app
import (
"context"
"github.com/arquivei/go-app/logger"
"github.com/rs/zerolog/log"
)
var (
// This is the default app.
defaultApp *App
)
type AppConfig interface {
GetAppConfig() Config
}
// Bootstrap initializes the config structure, the log and creates a new app internally.
func Bootstrap(appVersion string, config AppConfig) {
SetupConfig(config)
appConfig := config.GetAppConfig()
logger.Setup(appConfig.App.Log, appVersion)
log.Info().Str("config", logger.Flatten(config)).Msg("[app] Configuration loaded and global logger configured.")
defaultApp = New(appConfig)
}
// RunAndWait executes the main loop on a go-routine and listens to SIGINT and SIGKILL to start the shutdown.
// This is expected to be called only once and will panic if called a second time.
func RunAndWait(f MainLoopFunc) {
if defaultApp == nil {
panic("default app not initialized")
}
defaultApp.RunAndWait(f)
}
// Shutdown calls all shutdown methods ordered by priority.
// Handlers are processed from higher priority to lower priority.
func Shutdown(ctx context.Context) error {
if defaultApp == nil {
panic("default app not initialized")
}
return defaultApp.Shutdown(ctx)
}
// RegisterShutdownHandler adds a shutdown handler to the app. Shutdown Handlers are executed
// one at a time from the highest priority to the lowest priority. Shutdown handlers of the same
// priority are normally executed in the added order but this is not guaranteed.
func RegisterShutdownHandler(sh *ShutdownHandler) {
if defaultApp == nil {
panic("default app not initialized")
}
defaultApp.RegisterShutdownHandler(sh)
}
// ReadinessProbeGoup is a collection of readiness probes.
func ReadinessProbeGoup() *ProbeGroup {
if defaultApp == nil {
panic("default app not initialized")
}
return &defaultApp.Ready
}
// HealthinessProbeGroup is a colection of healthiness probes.
func HealthinessProbeGroup() *ProbeGroup {
if defaultApp == nil {
panic("default app not initialized")
}
return &defaultApp.Healthy
}