-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
108 lines (92 loc) · 2.29 KB
/
option.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
package micro
import (
"time"
"github.com/ofavor/micro-lite/client"
"github.com/ofavor/micro-lite/client/selector"
"github.com/ofavor/micro-lite/internal/log"
"github.com/ofavor/micro-lite/registry"
"github.com/ofavor/micro-lite/server"
)
// Options options for micro service
type Options struct {
Server server.Server
Client client.Client
}
// Option function to set micro service options
type Option func(opts *Options)
func defaultOptions() Options {
return Options{
Server: server.NewServer(),
Client: client.NewClient(),
}
}
// LogLevel set log level, supported levels: "debug", "info", "warn", "error", "fatal"
func LogLevel(lv string) Option {
return func(opts *Options) {
log.SetLevel(lv)
}
}
// ID set id
func ID(id string) Option {
return func(opts *Options) {
opts.Server.Init(server.ID(id))
}
}
// Name set name
func Name(name string) Option {
return func(opts *Options) {
opts.Server.Init(server.Name(name))
}
}
// Version set version. Must be in the format of "x.y.z"
func Version(ver string) Option {
return func(opts *Options) {
opts.Server.Init(server.Version(ver))
}
}
// Address set address
func Address(addr string) Option {
return func(opts *Options) {
opts.Server.Init(server.Address(addr))
}
}
// Registry set registry
func Registry(reg registry.Registry) Option {
return func(opts *Options) {
opts.Server.Init(server.Registry(reg))
opts.Client.Init(client.Registry(reg))
}
}
// RegistryAddrs set registry address
func RegistryAddrs(addrs []string) Option {
return func(opts *Options) {
opts.Server.Init(server.RegistryAddrs(addrs))
opts.Client.Init(client.RegistryAddrs(addrs))
}
}
// Selector set selector
func Selector(sel selector.Selector) Option {
return func(opts *Options) {
opts.Client.Init(client.Selector(sel))
}
}
// RegisterInterval set server register interval
func RegisterInterval(d time.Duration) Option {
return func(opts *Options) {
opts.Server.Init(server.RegisterInterval(d))
}
}
// RegisterTTL set server register TTL
func RegisterTTL(d time.Duration) Option {
return func(opts *Options) {
opts.Server.Init(server.RegisterTTL(d))
}
}
// WrapHandler wrap server handler
func WrapHandler(ws ...server.HandlerWrapper) Option {
return func(opts *Options) {
for _, w := range ws {
opts.Server.Init(server.WrapHandler(w))
}
}
}