-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
129 lines (105 loc) · 3.25 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
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
package lori
import (
"context"
"net/url"
"os"
"time"
"github.com/cr-mao/lori/log"
"github.com/cr-mao/lori/registry"
"github.com/cr-mao/lori/transport"
)
// Option is an application option.
type Option func(o *options)
// options is an application options.
type options struct {
id string
name string
version string
metadata map[string]string
endpoints []*url.URL // 暴露的地址
ctx context.Context // 上下文,可以传入进来,一般是不需要的。应该就是background context 出发的
sigs []os.Signal // 注册信号
logger log.Logger
registrar registry.Registrar // 服务注册
registrarTimeout time.Duration // 服务注册超时
stopTimeout time.Duration // 停止超时时间,可以给很大的值,看情况自己定
servers []transport.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 with service id.
func WithID(id string) Option {
return func(o *options) { o.id = id }
}
// Name with service name.
func WithName(name string) Option {
return func(o *options) { o.name = name }
}
// Version with service version.
func WithVersion(version string) Option {
return func(o *options) { o.version = version }
}
// Metadata with service metadata.
func WithMetadata(md map[string]string) Option {
return func(o *options) { o.metadata = md }
}
// Endpoint with service endpoint.
func WithEndpoint(endpoints ...*url.URL) Option {
return func(o *options) { o.endpoints = endpoints }
}
// Context with service context.
func WithContext(ctx context.Context) Option {
return func(o *options) { o.ctx = ctx }
}
// Logger with service logger.
func WithLogger(logger log.Logger) Option {
return func(o *options) { o.logger = logger }
}
// Server with transport servers.
func WithServer(srv ...transport.Server) Option {
return func(o *options) { o.servers = srv }
}
// Signal with exit signals.
func Signal(sigs ...os.Signal) Option {
return func(o *options) { o.sigs = sigs }
}
// Registrar with service registry.
func WithRegistrar(r registry.Registrar) Option {
return func(o *options) { o.registrar = r }
}
// RegistrarTimeout with registrar timeout.
func WithRegistrarTimeout(t time.Duration) Option {
return func(o *options) { o.registrarTimeout = t }
}
// StopTimeout with app stop timeout.
func WithStopTimeout(t time.Duration) Option {
return func(o *options) { o.stopTimeout = t }
}
// Before and Afters
// BeforeStart run funcs before app starts
func BeforeStart(fn func(context.Context) error) Option {
return func(o *options) {
o.beforeStart = append(o.beforeStart, fn)
}
}
// BeforeStop run funcs before app stops
func BeforeStop(fn func(context.Context) error) Option {
return func(o *options) {
o.beforeStop = append(o.beforeStop, fn)
}
}
// AfterStart run funcs after app starts
func AfterStart(fn func(context.Context) error) Option {
return func(o *options) {
o.afterStart = append(o.afterStart, fn)
}
}
// AfterStop run funcs after app stops
func AfterStop(fn func(context.Context) error) Option {
return func(o *options) {
o.afterStop = append(o.afterStop, fn)
}
}