-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
311 lines (279 loc) · 6.47 KB
/
application.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
/*
* Copyright (c) 2024 Mikhail Knyazhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package grape
import (
"go.osspkg.com/config"
"go.osspkg.com/console"
"go.osspkg.com/events"
config2 "go.osspkg.com/grape/config"
"go.osspkg.com/grape/container"
"go.osspkg.com/grape/env"
"go.osspkg.com/grape/internal"
"go.osspkg.com/grape/reflect"
"go.osspkg.com/logx"
"go.osspkg.com/xc"
)
type AppName string
type Grape interface {
Logger(log logx.Logger) Grape
Modules(modules ...interface{}) Grape
ConfigResolvers(res ...config.Resolver) Grape
ConfigFile(filename string) Grape
ConfigData(data, ext string) Grape
ConfigModels(configs ...interface{}) Grape
PidFile(filename string) Grape
Run()
Invoke(call interface{})
Call(call interface{})
ExitFunc(call func(code int)) Grape
}
type (
_conf struct {
FileName string
Data string
DataExt string
}
_grape struct {
appName string
config _conf
pidFilePath string
resolvers []config.Resolver
configs Modules
modules Modules
packages container.TContainer
logHandler *_log
logger logx.Logger
appContext xc.Context
exitFunc func(code int)
}
)
// New create application
func New(appName string) Grape {
ctx := xc.New()
return &_grape{
appName: appName,
resolvers: make([]config.Resolver, 0, 2),
modules: Modules{},
configs: Modules{},
packages: container.New(ctx),
appContext: ctx,
exitFunc: func(_ int) {},
}
}
// Logger setup logger
func (a *_grape) Logger(l logx.Logger) Grape {
a.logger = l
return a
}
// Modules append object to modules list
func (a *_grape) Modules(modules ...interface{}) Grape {
for _, mod := range modules {
switch v := mod.(type) {
case Modules:
a.modules = a.modules.Add(v...)
default:
a.modules = a.modules.Add(v)
}
}
return a
}
// ConfigFile set config file path
func (a *_grape) ConfigFile(filename string) Grape {
a.config = _conf{
FileName: filename,
}
return a
}
// ConfigData set config data
func (a *_grape) ConfigData(data, ext string) Grape {
a.config = _conf{
Data: data,
DataExt: ext,
}
return a
}
// ConfigModels set configs models
func (a *_grape) ConfigModels(configs ...interface{}) Grape {
for _, c := range configs {
a.configs = a.configs.Add(c)
}
return a
}
// ConfigResolvers set configs resolvers
func (a *_grape) ConfigResolvers(crs ...config.Resolver) Grape {
for _, r := range crs {
a.resolvers = append(a.resolvers, r)
}
return a
}
func (a *_grape) PidFile(filename string) Grape {
a.pidFilePath = filename
return a
}
func (a *_grape) ExitFunc(v func(code int)) Grape {
a.exitFunc = v
return a
}
// Run application with all dependencies
func (a *_grape) Run() {
a.prepareConfig(false)
result := a.steps(
[]step{
{
Message: "Registering dependencies",
Call: func() error { return a.packages.Register(a.modules...) },
},
{
Message: "Running dependencies",
Call: func() error { return a.packages.Start() },
},
},
func(er bool) {
if er {
a.appContext.Close()
return
}
go events.OnStopSignal(a.appContext.Close)
<-a.appContext.Done()
},
[]step{
{
Message: "Stop dependencies",
Call: func() error { return a.packages.Stop() },
},
},
)
console.FatalIfErr(a.logHandler.Close(), "close log file")
if result {
a.exitFunc(1)
}
a.exitFunc(0)
}
// Invoke run application with all dependencies and call function after starting
func (a *_grape) Invoke(call interface{}) {
a.prepareConfig(true)
result := a.steps(
[]step{
{
Call: func() error { return a.packages.Register(a.modules...) },
},
{
Call: func() error { return a.packages.Start() },
},
{
Call: func() error { return a.packages.Invoke(call) },
},
},
func(_ bool) {},
[]step{
{
Call: func() error { return a.packages.Stop() },
},
},
)
console.FatalIfErr(a.logHandler.Close(), "close log file")
if result {
a.exitFunc(1)
}
a.exitFunc(0)
}
// Call function with dependency and without starting all app
func (a *_grape) Call(call interface{}) {
a.prepareConfig(true)
result := a.steps(
[]step{
{
Call: func() error { return a.packages.Register(a.modules...) },
},
{
Call: func() error { return a.packages.Register(call) },
},
{
Call: func() error { return a.packages.BreakPoint(call) },
},
{
Call: func() error { return a.packages.Start() },
},
},
func(_ bool) {},
[]step{
{
Call: func() error { return a.packages.Stop() },
},
},
)
console.FatalIfErr(a.logHandler.Close(), "close log file")
if result {
a.exitFunc(1)
}
a.exitFunc(0)
}
func (a *_grape) prepareConfig(interactive bool) {
var err error
appConfig := config2.Default()
// read config file
resolver := config.New(a.resolvers...)
switch true {
case len(a.config.Data) > 0:
resolver.OpenBlob(a.config.Data, a.config.DataExt)
case len(a.config.FileName) > 0:
console.FatalIfErr(resolver.OpenFile(a.config.FileName), "Open config file: %s", a.config)
}
console.FatalIfErr(resolver.Build(), "Prepare config file: %s", a.config)
if !interactive {
console.FatalIfErr(resolver.Decode(appConfig), "Decode config file: %s", a.config)
}
// init logger
if a.logger == nil {
a.logger = logx.Default()
}
a.logHandler = initGlobalLogger(a.appName, appConfig.Log, a.logger)
// set env
a.modules = a.modules.Add(
env.ENV(appConfig.Env),
)
// decode all configs
var configs []interface{}
configs, err = reflect.TypingPtr(a.configs, func(c interface{}) error {
return resolver.Decode(c)
})
console.FatalIfErr(err, "Decode config file: %s", a.config)
a.modules = a.modules.Add(configs...)
if !interactive && len(a.pidFilePath) > 0 {
console.FatalIfErr(internal.SavePidToFile(a.pidFilePath), "Create pid file: %s", a.pidFilePath)
}
a.modules = a.modules.Add(
func() logx.Logger { return a.logger },
func() xc.Context { return a.appContext },
)
}
type step struct {
Call func() error
Message string
}
func (a *_grape) steps(up []step, wait func(bool), down []step) bool {
var erc int
for _, s := range up {
if len(s.Message) > 0 {
a.logger.Info(s.Message)
}
if err := s.Call(); err != nil {
a.logger.Error(s.Message, "err", err)
erc++
break
}
}
wait(erc > 0)
for _, s := range down {
if len(s.Message) > 0 {
a.logger.Info(s.Message)
}
if err := s.Call(); err != nil {
a.logger.Error(s.Message, "err", err)
erc++
}
}
return erc > 0
}