/
boot_manager.go
193 lines (144 loc) · 4 KB
/
boot_manager.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
package bootmanager
import (
"sync"
"github.com/labstack/echo/middleware"
"github.com/labstack/echo"
durationdata "github.com/IacopoMelani/Go-Starter-Project/pkg/models/duration_data"
"github.com/IacopoMelani/Go-Starter-Project/pkg/manager/db"
)
// BManager - Generalize a boot manager
type BManager interface {
StartApp()
RegisterDDataProc(func() *durationdata.DurationData)
RegisterProc(func())
RegisterEchoRoutes(func(e *echo.Echo))
SetAppPort(string)
SetDriverSQL(string)
SetConnectionSting(string)
UseEchoLogger()
UseEchoRecover()
}
// boot - Manage the app boot,
// implements BManager to impone the user to use the method GetBootManager to retrive the instance of boot
type boot struct {
appPort string
connection string
driverSQL string
durationDataRegistered []func() *durationdata.DurationData
e *echo.Echo
runnig bool
mu sync.Mutex
procRegistered []func()
wg sync.WaitGroup
}
// define the min cap for WaitGroup in the boot phase
const minWaitGroupCap = 2
var (
bmanger *boot
onceB = sync.Once{}
)
// initDbConnection - Initializes connection to database
func (b *boot) initDbConnection() {
db.InitConnection(b.driverSQL, b.connection)
}
// initDurationData - Initializes all DurationData registered before the the boot phase, to register a component use RegisterDDataProc
func (b *boot) initDurationData() {
for _, d := range b.durationDataRegistered {
durationdata.RegisterInitDurationData(d)
}
durationdata.InitDurationData()
}
// GetBootManager - Returns the one BManager
func GetBootManager() BManager {
onceB.Do(func() {
bmanger = new(boot)
bmanger.e = echo.New()
})
return bmanger
}
// startProc - Exec all generics procedure define before the boot phase, to register a proc use RegisterProc
func (b *boot) startProc() {
for _, f := range b.procRegistered {
go func(f func()) {
defer b.wg.Done()
f()
}(f)
}
}
// StartApp - Try to boot the app, exec all registered procedures and finaly start listeing on the designed port
func (b *boot) StartApp() {
b.wg.Add(minWaitGroupCap + len(b.procRegistered))
b.mu.Lock()
b.runnig = true
b.mu.Unlock()
if b.connection != "" && b.driverSQL != "" {
go func() {
defer b.wg.Done()
b.initDbConnection()
}()
} else {
b.wg.Done()
}
go func() {
defer b.wg.Done()
go b.initDurationData()
}()
b.startProc()
b.wg.Wait()
// start echo
b.e.Logger.Fatal(b.e.Start(b.appPort))
}
// RegisterDDataProc - Register a DurationData component
func (b *boot) RegisterDDataProc(f func() *durationdata.DurationData) {
b.mu.Lock()
defer b.mu.Unlock()
if b.runnig {
return
}
b.durationDataRegistered = append(b.durationDataRegistered, f)
}
// RegisterProc - Register a generics procedure, you can define whatever func you want, this procedure will start in the boot phase
func (b *boot) RegisterProc(f func()) {
b.mu.Lock()
defer b.mu.Unlock()
if b.runnig {
return
}
b.procRegistered = append(b.procRegistered, f)
}
// RegisterEchoRoutes - Register a func that register an Echo routes, this func will called in the boot phase
func (b *boot) RegisterEchoRoutes(f func(e *echo.Echo)) {
b.mu.Lock()
defer b.mu.Unlock()
f(b.e)
}
// SetAppPort - Set the listening port
func (b *boot) SetAppPort(appPort string) {
b.mu.Lock()
defer b.mu.Unlock()
b.appPort = appPort
}
// SetDriverSQL - Set the SQL driver
func (b *boot) SetDriverSQL(driver string) {
b.mu.Lock()
defer b.mu.Unlock()
b.driverSQL = driver
}
// SetConnectionSting - Set the connection string
func (b *boot) SetConnectionSting(conn string) {
b.mu.Lock()
defer b.mu.Unlock()
b.connection = conn
}
// UseEchoLogger - Declare that Echo will uses internal logger
func (b *boot) UseEchoLogger() {
b.mu.Lock()
defer b.mu.Unlock()
b.e.Use(middleware.Logger())
}
// UseEchoRecover - Declare that Echo uses the internal recover
func (b *boot) UseEchoRecover() {
b.mu.Lock()
defer b.mu.Unlock()
b.e.Use(middleware.Recover())
}