/
app.go
102 lines (78 loc) · 1.71 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
package app
import (
"context"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/dashotv/minion"
rift "github.com/dashotv/rift/client"
"github.com/dashotv/runic/reader"
)
var app *Application
type setupFunc func(app *Application) error
type healthFunc func(app *Application) error
var initializers = []setupFunc{setupConfig, setupLogger}
var healthchecks = map[string]healthFunc{}
var starters = []func(ctx context.Context, app *Application) error{}
type Application struct {
Config *Config
Log *zap.SugaredLogger
Reader *reader.Reader
Rift *rift.Client
Processor *Processor
//golem:template:app/app_partial_definitions
// DO NOT EDIT. This section is managed by github.com/dashotv/golem.
// Routes
Engine *echo.Echo
Default *echo.Group
Router *echo.Group
// Models
DB *Connector
// Events
Events *Events
// Workers
Workers *minion.Minion
//golem:template:app/app_partial_definitions
}
func Setup() error {
if app != nil {
return errors.New("application already setup")
}
app = &Application{}
for _, f := range initializers {
if err := f(app); err != nil {
return err
}
}
return nil
}
func Start() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if app == nil {
if err := Setup(); err != nil {
return err
}
}
for _, f := range starters {
if err := f(ctx, app); err != nil {
return err
}
}
app.Log.Info("started")
for {
select {
case <-ctx.Done():
app.Log.Info("stopping")
return nil
}
}
}
func (a *Application) Health() (map[string]bool, error) {
resp := make(map[string]bool)
for n, f := range healthchecks {
err := f(a)
resp[n] = err == nil
}
return resp, nil
}