Skip to content

Commit 2c1d03f

Browse files
author
dapeng
committed
Prepare -> Application
1 parent 6a1e629 commit 2c1d03f

14 files changed

+148
-136
lines changed

preparer.go renamed to application.go

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,30 @@ import (
66
"syscall"
77
)
88

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:
1127
// 1. Initialize signal channel
1228
// 2. Create new Core
1329
// 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{}
1733

1834
preparer.init()
1935
for _, fn := range loads {
@@ -25,23 +41,15 @@ func Prepare(loads ...LoadFunc) *Preparer {
2541
return &preparer
2642
}
2743

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
4046

41-
signal chan os.Signal
47+
// Prepare is alias for NewApp
48+
func Prepare(loads ...LoadFunc) *Application {
49+
return NewApp()
4250
}
4351

44-
func (s *Preparer) init() *Preparer {
52+
func (s *Application) init() *Application {
4553
s.signal = make(chan os.Signal, 1)
4654
s.loader = NewCore()
4755

@@ -54,7 +62,7 @@ func (s *Preparer) init() *Preparer {
5462
return s
5563
}
5664

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.
5866
// It wraps the Core.Load() method and panics if loading fails.
5967
//
6068
// Parameters:
@@ -69,29 +77,29 @@ func (s *Preparer) init() *Preparer {
6977
// - Order(order int): Set initialization order (lower runs first)
7078
// - FillWhenInit(): Fill dependencies during initialization
7179
//
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 {
7482
err := s.loader.Load(goner, options...)
7583
if err != nil {
7684
panic(err)
7785
}
7886
return s
7987
}
8088

81-
func Load(goner Goner, options ...Option) *Preparer {
89+
func Load(goner Goner, options ...Option) *Application {
8290
return Default.Load(goner, options...)
8391
}
8492

85-
// Loads executes multiple LoadFuncs in sequence to configure the Preparer
93+
// Loads executes multiple LoadFuncs in sequence to configure the Application
8694
// Parameters:
8795
// - loads: Variadic LoadFunc parameters that will be executed in order
8896
//
8997
// Each LoadFunc typically loads configurations or components.
9098
// If any LoadFunc fails during execution, it will trigger a panic.
9199
//
92100
// 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 {
95103
for _, fn := range loads {
96104
err := fn(s.loader)
97105
if err != nil {
@@ -101,81 +109,81 @@ func (s *Preparer) Loads(loads ...LoadFunc) *Preparer {
101109
return s
102110
}
103111

104-
func Loads(loads ...LoadFunc) *Preparer {
112+
func Loads(loads ...LoadFunc) *Application {
105113
return Default.Loads(loads...)
106114
}
107115

108116
// BeforeStart registers a function to be called before starting the application.
109117
// 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 {
112120
s.beforeStart(fn)
113121
return s
114122
}
115123

116-
func (s *Preparer) beforeStart(fn Process) {
124+
func (s *Application) beforeStart(fn Process) {
117125
s.beforeStartHooks = append([]Process{fn}, s.beforeStartHooks...)
118126
}
119127

120128
// AfterStart registers a function to be called after starting the application.
121129
// 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 {
124132
s.afterStart(fn)
125133
return s
126134
}
127135

128-
func (s *Preparer) afterStart(fn Process) {
136+
func (s *Application) afterStart(fn Process) {
129137
s.afterStartHooks = append(s.afterStartHooks, fn)
130138
}
131139

132140
// BeforeStop registers a function to be called before stopping the application.
133141
// 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 {
136144
s.beforeStop(fn)
137145
return s
138146
}
139147

140-
func (s *Preparer) beforeStop(fn Process) {
148+
func (s *Application) beforeStop(fn Process) {
141149
s.beforeStopHooks = append([]Process{fn}, s.beforeStopHooks...)
142150
}
143151

144152
// AfterStop registers a function to be called after stopping the application.
145153
// 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 {
148156
s.afterStop(fn)
149157
return s
150158
}
151159

152-
func (s *Preparer) afterStop(fn Process) {
160+
func (s *Application) afterStop(fn Process) {
153161
s.afterStopHooks = append(s.afterStopHooks, fn)
154162
}
155163

156164
// 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 {
159167
signal.Notify(s.signal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
160168
<-s.signal
161169
return s
162170
}
163171

164172
// 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 {
167175
s.signal <- syscall.SIGINT
168176
return s
169177
}
170178

171179
// 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
173181
// This is a convenience method equivalent to calling Default.End()
174182
func End() {
175183
Default.End()
176184
}
177185

178-
func (s *Preparer) start() {
186+
func (s *Application) start() {
179187
for _, fn := range s.beforeStartHooks {
180188
fn()
181189
}
@@ -192,7 +200,7 @@ func (s *Preparer) start() {
192200
}
193201
}
194202

195-
func (s *Preparer) stop() {
203+
func (s *Application) stop() {
196204
for _, fn := range s.beforeStopHooks {
197205
fn()
198206
}
@@ -209,7 +217,7 @@ func (s *Preparer) stop() {
209217
}
210218
}
211219

212-
func (s *Preparer) install() {
220+
func (s *Application) install() {
213221
err := s.loader.Install()
214222
if err != nil {
215223
panic(err)
@@ -223,7 +231,7 @@ func (s *Preparer) install() {
223231
//
224232
// Parameters:
225233
// - fn: The function to execute with injected dependencies
226-
func (s *Preparer) Run(fn ...any) {
234+
func (s *Application) Run(fn ...any) {
227235
s.install()
228236
s.start()
229237

@@ -243,7 +251,7 @@ func Run(fn any) {
243251

244252
// Serve initializes the application, starts all daemons, and waits for termination signal.
245253
// After receiving termination signal, performs cleanup by stopping all daemons.
246-
func (s *Preparer) Serve() {
254+
func (s *Application) Serve() {
247255
s.install()
248256
s.start()
249257
s.WaitEnd()
@@ -264,7 +272,7 @@ type testFlag struct {
264272

265273
func (*testFlag) forTest() {}
266274

267-
func (s *Preparer) Test(fn any) {
275+
func (s *Application) Test(fn any) {
268276
s.Load(&testFlag{})
269277
s.Run(fn)
270278
}
@@ -276,5 +284,5 @@ func Test(fn any) {
276284

277285
// RunTest Deprecated, use Test instead
278286
func RunTest(fn any, priests ...LoadFunc) {
279-
Prepare(priests...).Test(fn)
287+
NewApp(priests...).Test(fn)
280288
}

preparer_test.go renamed to application_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestPreparer_Lifecycle(t *testing.T) {
4444
hooksCalled = append(hooksCalled, name)
4545
}
4646

47-
preparer := gone.Prepare()
47+
preparer := gone.NewApp()
4848

4949
// Register hooks
5050
preparer.BeforeStart(func() {
@@ -117,7 +117,7 @@ func TestPreparer_DaemonErrors(t *testing.T) {
117117

118118
for _, tt := range tests {
119119
t.Run(tt.name, func(t *testing.T) {
120-
preparer := gone.Prepare()
120+
preparer := gone.NewApp()
121121
preparer.Load(tt.daemon)
122122

123123
if tt.wantPanic {
@@ -138,7 +138,7 @@ func TestPreparer_DaemonErrors(t *testing.T) {
138138
}
139139

140140
func TestPreparer_SignalHandling(t *testing.T) {
141-
preparer := gone.Prepare()
141+
preparer := gone.NewApp()
142142

143143
// Test SIGINT
144144
go func() {
@@ -156,7 +156,7 @@ func TestPreparer_SignalHandling(t *testing.T) {
156156
}
157157

158158
func TestPreparer_MultipleHooks(t *testing.T) {
159-
preparer := gone.Prepare()
159+
preparer := gone.NewApp()
160160
var counter int
161161
var mu sync.Mutex
162162

@@ -189,20 +189,20 @@ func TestPreparer_MultipleHooks(t *testing.T) {
189189
func TestPreparer_LoadErrors(t *testing.T) {
190190
tests := []struct {
191191
name string
192-
setup func(*gone.Preparer)
192+
setup func(*gone.Application)
193193
wantPanic bool
194194
}{
195195
{
196196
name: "Duplicate named component",
197-
setup: func(p *gone.Preparer) {
197+
setup: func(p *gone.Application) {
198198
p.Load(&Worker{name: "test"})
199199
p.Load(&Worker{name: "test"})
200200
},
201201
wantPanic: true,
202202
},
203203
{
204204
name: "Valid components",
205-
setup: func(p *gone.Preparer) {
205+
setup: func(p *gone.Application) {
206206
p.Load(&Worker{name: "worker1"})
207207
p.Load(&Worker{name: "worker2"})
208208
},
@@ -212,7 +212,7 @@ func TestPreparer_LoadErrors(t *testing.T) {
212212

213213
for _, tt := range tests {
214214
t.Run(tt.name, func(t *testing.T) {
215-
preparer := gone.Prepare()
215+
preparer := gone.NewApp()
216216
didPanic := false
217217
func() {
218218
defer func() {
@@ -230,7 +230,7 @@ func TestPreparer_LoadErrors(t *testing.T) {
230230
}
231231

232232
func TestPreparer_RunWithDependencies(t *testing.T) {
233-
preparer := gone.Prepare()
233+
preparer := gone.NewApp()
234234

235235
worker1 := &Worker{name: "worker1"}
236236
worker2 := &Worker{name: "worker2"}
@@ -286,7 +286,7 @@ func TestPreparer_DefaultInstance(t *testing.T) {
286286
}
287287

288288
func TestPreparer_Loads(t *testing.T) {
289-
preparer := gone.Prepare()
289+
preparer := gone.NewApp()
290290

291291
// Test successful loads
292292
loadFn1 := func(core gone.Loader) error {
@@ -335,7 +335,7 @@ func TestPreparer_Test(t *testing.T) {
335335
testFuncCalled = true
336336
}
337337

338-
preparer := gone.Prepare()
338+
preparer := gone.NewApp()
339339
preparer.Test(testFunc)
340340

341341
if !testFuncCalled {
@@ -433,18 +433,18 @@ func TestPreparer_PrepareWithLoads(t *testing.T) {
433433
return core.Load(&Worker{name: "worker2"})
434434
}
435435

436-
var preparer *gone.Preparer
436+
var preparer *gone.Application
437437
func() {
438438
defer func() {
439439
if r := recover(); r != nil {
440440
t.Errorf("Unexpected panic: %v", r)
441441
}
442442
}()
443-
preparer = gone.Prepare(loadFn1, loadFn2)
443+
preparer = gone.NewApp(loadFn1, loadFn2)
444444
}()
445445

446446
if preparer == nil {
447-
t.Error("Preparer should not be nil")
447+
t.Error("Application should not be nil")
448448
}
449449

450450
// Test prepare with error load function
@@ -459,11 +459,11 @@ func TestPreparer_PrepareWithLoads(t *testing.T) {
459459
didPanic = true
460460
}
461461
}()
462-
gone.Prepare(errorLoadFn)
462+
gone.NewApp(errorLoadFn)
463463
}()
464464

465465
if !didPanic {
466-
t.Error("Expected Prepare to panic with error load function")
466+
t.Error("Expected NewApp to panic with error load function")
467467
}
468468
}
469469

0 commit comments

Comments
 (0)