-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapplication.go
288 lines (243 loc) · 6.98 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
package gone
import (
"os"
"os/signal"
"syscall"
)
type Application struct {
Flag
loader *Core `gone:"*"`
daemons []Daemon `gone:"*"`
beforeStartHooks []Process
afterStartHooks []Process
beforeStopHooks []Process
afterStopHooks []Process
signal chan os.Signal
}
var Default = NewApp()
// NewApp creates and initializes a new Application instance.
// It creates an empty Application struct and calls init() to:
// 1. Initialize signal channel
// 2. Create new Core
// 3. Load core components like providers and default configure
// Returns the initialized Application instance ready for use.
func NewApp(loads ...LoadFunc) *Application {
preparer := Application{}
preparer.init()
for _, fn := range loads {
err := fn(preparer.loader)
if err != nil {
panic(err)
}
}
return &preparer
}
// Preparer is a type alias for Application, representing the main entry point for application setup and execution.
type Preparer = Application
// Prepare is alias for NewApp
func Prepare(loads ...LoadFunc) *Application {
return NewApp(loads...)
}
func (s *Application) init() *Application {
s.signal = make(chan os.Signal, 1)
s.loader = NewCore()
s.
Load(s, IsDefault()).
Load(&BeforeStartProvider{}).
Load(&AfterStartProvider{}).
Load(&BeforeStopProvider{}).
Load(&AfterStopProvider{})
return s
}
// Load loads a Goner into the Application's loader with optional configuration options.
// It wraps the Core.Load() method and panics if loading fails.
//
// Parameters:
// - goner: The Goner instance to load
// - options: Optional configuration options for the Goner
//
// Available Options:
// - Name(name string): Set custom name for the Goner
// - IsDefault(): Mark this Goner as the default implementation
// - OnlyForName(): Only register by name, not as provider
// - ForceReplace(): Replace existing Goner with same name/type
// - Order(order int): Set initialization order (lower runs first)
// - FillWhenInit(): Fill dependencies during initialization
//
// Returns the Application instance for method chaining
func (s *Application) Load(goner Goner, options ...Option) *Application {
err := s.loader.Load(goner, options...)
if err != nil {
panic(err)
}
return s
}
func Load(goner Goner, options ...Option) *Application {
return Default.Load(goner, options...)
}
// Loads executes multiple LoadFuncs in sequence to configure the Application
// Parameters:
// - loads: Variadic LoadFunc parameters that will be executed in order
//
// Each LoadFunc typically loads configurations or components.
// If any LoadFunc fails during execution, it will trigger a panic.
//
// Returns:
// - *Application: Returns the Application instance itself for method chaining
func (s *Application) Loads(loads ...LoadFunc) *Application {
for _, fn := range loads {
err := fn(s.loader)
if err != nil {
panic(err)
}
}
return s
}
func Loads(loads ...LoadFunc) *Application {
return Default.Loads(loads...)
}
// BeforeStart registers a function to be called before starting the application.
// The function will be executed before any daemons are started.
// Returns the Application instance for method chaining.
func (s *Application) BeforeStart(fn Process) *Application {
s.beforeStart(fn)
return s
}
func (s *Application) beforeStart(fn Process) {
s.beforeStartHooks = append([]Process{fn}, s.beforeStartHooks...)
}
// AfterStart registers a function to be called after starting the application.
// The function will be executed after all daemons have been started.
// Returns the Application instance for method chaining.
func (s *Application) AfterStart(fn Process) *Application {
s.afterStart(fn)
return s
}
func (s *Application) afterStart(fn Process) {
s.afterStartHooks = append(s.afterStartHooks, fn)
}
// BeforeStop registers a function to be called before stopping the application.
// The function will be executed before any daemons are stopped.
// Returns the Application instance for method chaining.
func (s *Application) BeforeStop(fn Process) *Application {
s.beforeStop(fn)
return s
}
func (s *Application) beforeStop(fn Process) {
s.beforeStopHooks = append([]Process{fn}, s.beforeStopHooks...)
}
// AfterStop registers a function to be called after stopping the application.
// The function will be executed after all daemons have been stopped.
// Returns the Application instance for method chaining.
func (s *Application) AfterStop(fn Process) *Application {
s.afterStop(fn)
return s
}
func (s *Application) afterStop(fn Process) {
s.afterStopHooks = append(s.afterStopHooks, fn)
}
// WaitEnd blocks until the application receives a termination signal (SIGINT, SIGTERM, or SIGQUIT).
// Returns the Application instance for method chaining.
func (s *Application) WaitEnd() *Application {
signal.Notify(s.signal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-s.signal
return s
}
// End triggers application termination by sending a SIGINT signal.
// Returns the Application instance for method chaining.
func (s *Application) End() *Application {
s.signal <- syscall.SIGINT
return s
}
// End triggers application termination
// It terminates the application by sending a SIGINT signal to the default Application instance
// This is a convenience method equivalent to calling Default.End()
func End() {
Default.End()
}
func (s *Application) start() {
for _, fn := range s.beforeStartHooks {
fn()
}
for _, daemon := range s.daemons {
err := daemon.Start()
if err != nil {
panic(err)
}
}
for _, fn := range s.afterStartHooks {
fn()
}
}
func (s *Application) stop() {
for _, fn := range s.beforeStopHooks {
fn()
}
for i := len(s.daemons) - 1; i >= 0; i-- {
err := s.daemons[i].Stop()
if err != nil {
panic(err)
}
}
for _, fn := range s.afterStopHooks {
fn()
}
}
func (s *Application) install() {
err := s.loader.Install()
if err != nil {
panic(err)
}
}
// Run initializes the application, injects dependencies into the provided function,
// executes it, and then performs cleanup.
// The function can have dependencies that will be automatically injected.
// Panics if dependency injection or execution fails.
//
// Parameters:
// - fn: The function to execute with injected dependencies
func (s *Application) Run(fn ...any) {
s.install()
s.start()
for _, fn := range fn {
f, err := s.loader.InjectWrapFunc(fn, nil, nil)
if err != nil {
panic(err)
}
_ = f()
}
s.stop()
}
func Run(fn any) {
Default.Run(fn)
}
// Serve initializes the application, starts all daemons, and waits for termination signal.
// After receiving termination signal, performs cleanup by stopping all daemons.
func (s *Application) Serve() {
s.install()
s.start()
s.WaitEnd()
s.stop()
}
func Serve() {
Default.Serve()
}
type TestFlag interface {
forTest()
}
type testFlag struct {
Flag
}
func (*testFlag) forTest() {}
func (s *Application) Test(fn any) {
s.Load(&testFlag{})
s.Run(fn)
}
// Test for run tests
func Test(fn any) {
Default.Test(fn)
}
// RunTest Deprecated, use Test instead
func RunTest(fn any, priests ...LoadFunc) {
NewApp(priests...).Test(fn)
}