-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
96 lines (76 loc) · 2.06 KB
/
options.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
package toho
import (
"context"
"time"
"github.com/go-toho/toho/app"
)
// Option is an application option.
type Option func(o *options)
// options is an application options.
type options struct {
appInfoOpts []app.Option
ctx context.Context
core Core
logger any
options []any
startTimeout time.Duration
stopTimeout time.Duration
// Lifecycle functions
beforeStart []func(context.Context) error
afterStart []func(context.Context) error
beforeStop []func(context.Context) error
afterStop []func(context.Context) error
}
// AppInfo with app info.
func AppInfo(opts ...app.Option) Option {
return func(o *options) { o.appInfoOpts = opts }
}
// AppCore with app core.
func AppCore(s Core) Option {
return func(o *options) { o.core = s }
}
// Context with app context.
func Context(ctx context.Context) Option {
return func(o *options) { o.ctx = ctx }
}
// Logger with app logger.
func Logger(l any) Option {
return func(o *options) { o.logger = l }
}
// Options with any options for the app.
func Options(opts ...any) Option {
return func(o *options) { o.options = opts }
}
// StartTimeout with app start timeout.
func StartTimeout(t time.Duration) Option {
return func(o *options) { o.startTimeout = t }
}
// StopTimeout with app stop timeout.
func StopTimeout(t time.Duration) Option {
return func(o *options) { o.stopTimeout = t }
}
// Lifecycle functions
// BeforeStart run functions before app starts.
func BeforeStart(fn func(context.Context) error) Option {
return func(o *options) {
o.beforeStart = append(o.beforeStart, fn)
}
}
// AfterStart run functions after app starts.
func AfterStart(fn func(context.Context) error) Option {
return func(o *options) {
o.afterStart = append(o.afterStart, fn)
}
}
// BeforeStop run functions before app stops.
func BeforeStop(fn func(context.Context) error) Option {
return func(o *options) {
o.beforeStop = append(o.beforeStop, fn)
}
}
// AfterStop run functions after app stops.
func AfterStop(fn func(context.Context) error) Option {
return func(o *options) {
o.afterStop = append(o.afterStop, fn)
}
}