-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.go
More file actions
106 lines (85 loc) · 2.5 KB
/
Copy pathoption.go
File metadata and controls
106 lines (85 loc) · 2.5 KB
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
package dove
import (
"context"
"os"
"time"
"github.com/camry/g/v2/glog"
"github.com/camry/dove/v2/server"
)
// Option 定义一个应用程序选项类型。
type Option func(o *option)
// option 应用程序选项实体对象。
type option struct {
id string
name string
version string
ctx context.Context
sigs []os.Signal
logger glog.Logger
stopTimeout time.Duration
servers []server.Server
// Before and After funcs
beforeStart []func(context.Context) error
beforeStop []func(context.Context) error
afterStart []func(context.Context) error
afterStop []func(context.Context) error
}
// ID 配置服务ID。
func ID(id string) Option {
return func(o *option) { o.id = id }
}
// Name 配置服务名称。
func Name(name string) Option {
return func(o *option) { o.name = name }
}
// Version 配置服务版本。
func Version(version string) Option {
return func(o *option) { o.version = version }
}
// Context 配置服务上下文。
func Context(ctx context.Context) Option {
return func(o *option) { o.ctx = ctx }
}
// Signal 配置服务信号。
func Signal(signals ...os.Signal) Option {
return func(o *option) { o.sigs = signals }
}
// Logger 配置日志记录器。
func Logger(logger glog.Logger) Option {
return func(o *option) { o.logger = logger }
}
// StopTimeout 配置应用停止超时时间(单位:秒)。
func StopTimeout(t time.Duration) Option {
return func(o *option) { o.stopTimeout = t }
}
// Server 配置服务器。
func Server(srv ...server.Server) Option {
return func(o *option) { o.servers = srv }
}
/**********************************/
/******** Before and After ********/
/**********************************/
// BeforeStart 应用启动前执行此 funcs。
func BeforeStart(fn func(context.Context) error) Option {
return func(o *option) {
o.beforeStart = append(o.beforeStart, fn)
}
}
// BeforeStop 应用停止前执行此 funcs。
func BeforeStop(fn func(context.Context) error) Option {
return func(o *option) {
o.beforeStop = append(o.beforeStop, fn)
}
}
// AfterStart 应用启动后执行此 funcs。
func AfterStart(fn func(context.Context) error) Option {
return func(o *option) {
o.afterStart = append(o.afterStart, fn)
}
}
// AfterStop 应用停止后执行此 funcs。
func AfterStop(fn func(context.Context) error) Option {
return func(o *option) {
o.afterStop = append(o.afterStop, fn)
}
}