-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
95 lines (78 loc) · 2.23 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
package registry
import (
"os"
"github.com/rs/xid"
)
// Option represents the options for registry server.
type Option struct {
// Id is the service ID.
Id string
// Bind is the address used to register the service.
// If there is a firewall, ensure that the port is open for both TCP and UDP.
Bind string
// BindAdvertise is the address that the service will advertise to other services for registering.
// Can be used for basic NAT traversal where both the internal IP:port and external IP:port are known.
BindAdvertise string
// Registries are the addresses of other registry servers.
// If there are more than one, separate them with commas, such as "192.168.1.101:7370,192.168.1.102:7370".
Registries string
// Addr is the address used for service discovery.
Addr string
// Advertise is the address that will be advertised to clients for service discovery.
Advertise string
}
// IOption represents a function that modifies the Option.
type IOption func(o *Option)
// OptId sets the service ID option.
func OptId(id string) IOption {
return func(o *Option) {
if id != "" {
o.Id = id
}
}
}
// OptAddr sets the service discovery address option.
func OptAddr(addr string) IOption {
return func(o *Option) {
o.Addr = addr
}
}
// OptAdvertise sets the advertised address for service discovery option.
func OptAdvertise(addr string) IOption {
return func(o *Option) {
o.Advertise = addr
}
}
// OptBindAdvertise sets the advertised address for service registration option.
func OptBindAdvertise(addr string) IOption {
return func(o *Option) {
if addr != "" {
o.BindAdvertise = addr
}
}
}
// OptBind sets the address used for service registration option.
func OptBind(addr string) IOption {
return func(o *Option) {
if addr != "" {
o.Bind = addr
}
}
}
// OptRegistries sets the addresses of other registry servers option.
func OptRegistries(registries string) IOption {
return func(o *Option) {
if registries != "" {
o.Registries = registries
}
}
}
// DefaultOption returns the default options for registering a server.
func DefaultOption() *Option {
hostname, _ := os.Hostname()
return &Option{
Id: hostname + "-" + xid.New().String(),
Bind: ":7370",
BindAdvertise: ":7370",
}
}