-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
112 lines (97 loc) · 1.8 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
103
104
105
106
107
108
109
110
111
112
package flam
import (
"io"
"go.uber.org/dig"
)
// Provider @todo doc
type Provider interface {
ID() string
Reg(a App) error
}
// Bootable @todo doc
type Bootable interface {
Boot() error
}
// App @todo doc
type App interface {
io.Closer
DI() *dig.Container
Provide(p Provider) error
Boot() error
Run() error
}
// NewApp @todo doc
func NewApp() App {
a := &app{
di: dig.New(),
providers: []Provider{},
isBooted: false,
}
_ = a.Provide(newFsProvider())
_ = a.Provide(newTriggerProvider())
_ = a.Provide(newConfigProvider())
_ = a.Provide(newLogProvider())
_ = a.Provide(newRdbProvider())
_ = a.Provide(newMigrationProvider())
_ = a.Provide(newValidationProvider())
_ = a.Provide(newRestProvider())
_ = a.Provide(newEnvelopeRestMwProvider())
_ = a.Provide(newLogRestMwProvider())
_ = a.Provide(newWatchdogProvider())
return a
}
type app struct {
di *dig.Container
providers []Provider
isBooted bool
}
func (a *app) Close() error {
for _, p := range a.providers {
if closer, ok := p.(io.Closer); ok {
if e := closer.Close(); e != nil {
return e
}
}
}
return nil
}
func (a *app) DI() *dig.Container {
return a.di
}
func (a *app) Provide(p Provider) error {
id := p.ID()
for _, r := range a.providers {
if r.ID() == id {
return NewError("duplicate provider", Bag{"id": id})
}
}
a.providers = append(a.providers, p)
return p.Reg(a)
}
func (a *app) Boot() error {
if a.isBooted {
return nil
}
for _, p := range a.providers {
if bootable, ok := p.(Bootable); ok {
if e := bootable.Boot(); e != nil {
return e
}
}
}
a.isBooted = true
return nil
}
func (a *app) Run() error {
if e := a.Boot(); e != nil {
return e
}
if e := a.di.Invoke(
func(k WatchdogKennel) error {
return k.Run()
},
); e != nil {
return e
}
return nil
}