@@ -6,14 +6,30 @@ import (
6
6
"syscall"
7
7
)
8
8
9
- // Prepare creates and initializes a new Preparer instance.
10
- // It creates an empty Preparer struct and calls init() to:
9
+ type Application struct {
10
+ Flag
11
+
12
+ loader * Core `gone:"*"`
13
+ daemons []Daemon `gone:"*"`
14
+
15
+ beforeStartHooks []Process
16
+ afterStartHooks []Process
17
+ beforeStopHooks []Process
18
+ afterStopHooks []Process
19
+
20
+ signal chan os.Signal
21
+ }
22
+
23
+ var Default = NewApp ()
24
+
25
+ // NewApp creates and initializes a new Application instance.
26
+ // It creates an empty Application struct and calls init() to:
11
27
// 1. Initialize signal channel
12
28
// 2. Create new Core
13
29
// 3. Load core components like providers and default configure
14
- // Returns the initialized Preparer instance ready for use.
15
- func Prepare (loads ... LoadFunc ) * Preparer {
16
- preparer := Preparer {}
30
+ // Returns the initialized Application instance ready for use.
31
+ func NewApp (loads ... LoadFunc ) * Application {
32
+ preparer := Application {}
17
33
18
34
preparer .init ()
19
35
for _ , fn := range loads {
@@ -25,23 +41,15 @@ func Prepare(loads ...LoadFunc) *Preparer {
25
41
return & preparer
26
42
}
27
43
28
- var Default = Prepare ()
29
-
30
- type Preparer struct {
31
- Flag
32
-
33
- loader * Core `gone:"*"`
34
- daemons []Daemon `gone:"*"`
35
-
36
- beforeStartHooks []Process
37
- afterStartHooks []Process
38
- beforeStopHooks []Process
39
- afterStopHooks []Process
44
+ // Preparer is a type alias for Application, representing the main entry point for application setup and execution.
45
+ type Preparer = Application
40
46
41
- signal chan os.Signal
47
+ // Prepare is alias for NewApp
48
+ func Prepare (loads ... LoadFunc ) * Application {
49
+ return NewApp ()
42
50
}
43
51
44
- func (s * Preparer ) init () * Preparer {
52
+ func (s * Application ) init () * Application {
45
53
s .signal = make (chan os.Signal , 1 )
46
54
s .loader = NewCore ()
47
55
@@ -54,7 +62,7 @@ func (s *Preparer) init() *Preparer {
54
62
return s
55
63
}
56
64
57
- // Load loads a Goner into the Preparer 's loader with optional configuration options.
65
+ // Load loads a Goner into the Application 's loader with optional configuration options.
58
66
// It wraps the Core.Load() method and panics if loading fails.
59
67
//
60
68
// Parameters:
@@ -69,29 +77,29 @@ func (s *Preparer) init() *Preparer {
69
77
// - Order(order int): Set initialization order (lower runs first)
70
78
// - FillWhenInit(): Fill dependencies during initialization
71
79
//
72
- // Returns the Preparer instance for method chaining
73
- func (s * Preparer ) Load (goner Goner , options ... Option ) * Preparer {
80
+ // Returns the Application instance for method chaining
81
+ func (s * Application ) Load (goner Goner , options ... Option ) * Application {
74
82
err := s .loader .Load (goner , options ... )
75
83
if err != nil {
76
84
panic (err )
77
85
}
78
86
return s
79
87
}
80
88
81
- func Load (goner Goner , options ... Option ) * Preparer {
89
+ func Load (goner Goner , options ... Option ) * Application {
82
90
return Default .Load (goner , options ... )
83
91
}
84
92
85
- // Loads executes multiple LoadFuncs in sequence to configure the Preparer
93
+ // Loads executes multiple LoadFuncs in sequence to configure the Application
86
94
// Parameters:
87
95
// - loads: Variadic LoadFunc parameters that will be executed in order
88
96
//
89
97
// Each LoadFunc typically loads configurations or components.
90
98
// If any LoadFunc fails during execution, it will trigger a panic.
91
99
//
92
100
// Returns:
93
- // - *Preparer : Returns the Preparer instance itself for method chaining
94
- func (s * Preparer ) Loads (loads ... LoadFunc ) * Preparer {
101
+ // - *Application : Returns the Application instance itself for method chaining
102
+ func (s * Application ) Loads (loads ... LoadFunc ) * Application {
95
103
for _ , fn := range loads {
96
104
err := fn (s .loader )
97
105
if err != nil {
@@ -101,81 +109,81 @@ func (s *Preparer) Loads(loads ...LoadFunc) *Preparer {
101
109
return s
102
110
}
103
111
104
- func Loads (loads ... LoadFunc ) * Preparer {
112
+ func Loads (loads ... LoadFunc ) * Application {
105
113
return Default .Loads (loads ... )
106
114
}
107
115
108
116
// BeforeStart registers a function to be called before starting the application.
109
117
// The function will be executed before any daemons are started.
110
- // Returns the Preparer instance for method chaining.
111
- func (s * Preparer ) BeforeStart (fn Process ) * Preparer {
118
+ // Returns the Application instance for method chaining.
119
+ func (s * Application ) BeforeStart (fn Process ) * Application {
112
120
s .beforeStart (fn )
113
121
return s
114
122
}
115
123
116
- func (s * Preparer ) beforeStart (fn Process ) {
124
+ func (s * Application ) beforeStart (fn Process ) {
117
125
s .beforeStartHooks = append ([]Process {fn }, s .beforeStartHooks ... )
118
126
}
119
127
120
128
// AfterStart registers a function to be called after starting the application.
121
129
// The function will be executed after all daemons have been started.
122
- // Returns the Preparer instance for method chaining.
123
- func (s * Preparer ) AfterStart (fn Process ) * Preparer {
130
+ // Returns the Application instance for method chaining.
131
+ func (s * Application ) AfterStart (fn Process ) * Application {
124
132
s .afterStart (fn )
125
133
return s
126
134
}
127
135
128
- func (s * Preparer ) afterStart (fn Process ) {
136
+ func (s * Application ) afterStart (fn Process ) {
129
137
s .afterStartHooks = append (s .afterStartHooks , fn )
130
138
}
131
139
132
140
// BeforeStop registers a function to be called before stopping the application.
133
141
// The function will be executed before any daemons are stopped.
134
- // Returns the Preparer instance for method chaining.
135
- func (s * Preparer ) BeforeStop (fn Process ) * Preparer {
142
+ // Returns the Application instance for method chaining.
143
+ func (s * Application ) BeforeStop (fn Process ) * Application {
136
144
s .beforeStop (fn )
137
145
return s
138
146
}
139
147
140
- func (s * Preparer ) beforeStop (fn Process ) {
148
+ func (s * Application ) beforeStop (fn Process ) {
141
149
s .beforeStopHooks = append ([]Process {fn }, s .beforeStopHooks ... )
142
150
}
143
151
144
152
// AfterStop registers a function to be called after stopping the application.
145
153
// The function will be executed after all daemons have been stopped.
146
- // Returns the Preparer instance for method chaining.
147
- func (s * Preparer ) AfterStop (fn Process ) * Preparer {
154
+ // Returns the Application instance for method chaining.
155
+ func (s * Application ) AfterStop (fn Process ) * Application {
148
156
s .afterStop (fn )
149
157
return s
150
158
}
151
159
152
- func (s * Preparer ) afterStop (fn Process ) {
160
+ func (s * Application ) afterStop (fn Process ) {
153
161
s .afterStopHooks = append (s .afterStopHooks , fn )
154
162
}
155
163
156
164
// WaitEnd blocks until the application receives a termination signal (SIGINT, SIGTERM, or SIGQUIT).
157
- // Returns the Preparer instance for method chaining.
158
- func (s * Preparer ) WaitEnd () * Preparer {
165
+ // Returns the Application instance for method chaining.
166
+ func (s * Application ) WaitEnd () * Application {
159
167
signal .Notify (s .signal , syscall .SIGINT , syscall .SIGTERM , syscall .SIGQUIT )
160
168
<- s .signal
161
169
return s
162
170
}
163
171
164
172
// End triggers application termination by sending a SIGINT signal.
165
- // Returns the Preparer instance for method chaining.
166
- func (s * Preparer ) End () * Preparer {
173
+ // Returns the Application instance for method chaining.
174
+ func (s * Application ) End () * Application {
167
175
s .signal <- syscall .SIGINT
168
176
return s
169
177
}
170
178
171
179
// End triggers application termination
172
- // It terminates the application by sending a SIGINT signal to the default Preparer instance
180
+ // It terminates the application by sending a SIGINT signal to the default Application instance
173
181
// This is a convenience method equivalent to calling Default.End()
174
182
func End () {
175
183
Default .End ()
176
184
}
177
185
178
- func (s * Preparer ) start () {
186
+ func (s * Application ) start () {
179
187
for _ , fn := range s .beforeStartHooks {
180
188
fn ()
181
189
}
@@ -192,7 +200,7 @@ func (s *Preparer) start() {
192
200
}
193
201
}
194
202
195
- func (s * Preparer ) stop () {
203
+ func (s * Application ) stop () {
196
204
for _ , fn := range s .beforeStopHooks {
197
205
fn ()
198
206
}
@@ -209,7 +217,7 @@ func (s *Preparer) stop() {
209
217
}
210
218
}
211
219
212
- func (s * Preparer ) install () {
220
+ func (s * Application ) install () {
213
221
err := s .loader .Install ()
214
222
if err != nil {
215
223
panic (err )
@@ -223,7 +231,7 @@ func (s *Preparer) install() {
223
231
//
224
232
// Parameters:
225
233
// - fn: The function to execute with injected dependencies
226
- func (s * Preparer ) Run (fn ... any ) {
234
+ func (s * Application ) Run (fn ... any ) {
227
235
s .install ()
228
236
s .start ()
229
237
@@ -243,7 +251,7 @@ func Run(fn any) {
243
251
244
252
// Serve initializes the application, starts all daemons, and waits for termination signal.
245
253
// After receiving termination signal, performs cleanup by stopping all daemons.
246
- func (s * Preparer ) Serve () {
254
+ func (s * Application ) Serve () {
247
255
s .install ()
248
256
s .start ()
249
257
s .WaitEnd ()
@@ -264,7 +272,7 @@ type testFlag struct {
264
272
265
273
func (* testFlag ) forTest () {}
266
274
267
- func (s * Preparer ) Test (fn any ) {
275
+ func (s * Application ) Test (fn any ) {
268
276
s .Load (& testFlag {})
269
277
s .Run (fn )
270
278
}
@@ -276,5 +284,5 @@ func Test(fn any) {
276
284
277
285
// RunTest Deprecated, use Test instead
278
286
func RunTest (fn any , priests ... LoadFunc ) {
279
- Prepare (priests ... ).Test (fn )
287
+ NewApp (priests ... ).Test (fn )
280
288
}
0 commit comments