-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
144 lines (123 loc) · 3.32 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package ngrpc
import (
"os"
"github.com/nilorg/ngrpc/v2/resolver"
"google.golang.org/grpc"
)
// ServerOptions 可选参数列表
type ServerOptions struct {
Name string
Address string
Log Logger
StreamServerInterceptors []grpc.StreamServerInterceptor
UnaryServerInterceptors []grpc.UnaryServerInterceptor
register resolver.Registry
RandomPort bool
}
// ServerOption 为可选参数赋值的函数
type ServerOption func(*ServerOptions)
// NewServerOptions 创建可选参数
func NewServerOptions(opts ...ServerOption) ServerOptions {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
opt := ServerOptions{
Name: hostname,
Address: ":5000",
RandomPort: false,
Log: new(StdLogger),
}
for _, o := range opts {
o(&opt)
}
return opt
}
func WithServerName(name string) ServerOption {
return func(o *ServerOptions) {
o.Name = name
}
}
func WithServerAddress(address string) ServerOption {
return func(o *ServerOptions) {
o.Address = address
}
}
func WithServerLogger(log Logger) ServerOption {
return func(o *ServerOptions) {
o.Log = log
}
}
func WithServerStreamServerInterceptors(streamServerInterceptors ...grpc.StreamServerInterceptor) ServerOption {
return func(o *ServerOptions) {
o.StreamServerInterceptors = streamServerInterceptors
}
}
func WithServerUnaryServerInterceptors(unaryServerInterceptors ...grpc.UnaryServerInterceptor) ServerOption {
return func(o *ServerOptions) {
o.UnaryServerInterceptors = unaryServerInterceptors
}
}
func WithServerRegister(register resolver.Registry) ServerOption {
return func(o *ServerOptions) {
o.register = register
}
}
func WithServerRandomPort(randomPort bool) ServerOption {
return func(o *ServerOptions) {
o.RandomPort = randomPort
}
}
// ClientOptions 可选参数列表
type ClientOptions struct {
Name string
Address string
Log Logger
StreamClientInterceptors []grpc.StreamClientInterceptor
UnaryClientInterceptors []grpc.UnaryClientInterceptor
discovery resolver.Discovery
}
// ClientOption 为可选参数赋值的函数
type ClientOption func(*ClientOptions)
// NewClientOptions 创建可选参数
func NewClientOptions(opts ...ClientOption) ClientOptions {
opt := ClientOptions{
Name: "unknown",
Address: ":5000",
Log: new(StdLogger),
}
for _, o := range opts {
o(&opt)
}
return opt
}
func WithClientName(name string) ClientOption {
return func(o *ClientOptions) {
o.Name = name
}
}
func WithClientAddress(address string) ClientOption {
return func(o *ClientOptions) {
o.Address = address
}
}
func WithClientLogger(log Logger) ClientOption {
return func(o *ClientOptions) {
o.Log = log
}
}
func WithClientStreamClientInterceptors(streamClientInterceptors ...grpc.StreamClientInterceptor) ClientOption {
return func(o *ClientOptions) {
o.StreamClientInterceptors = streamClientInterceptors
}
}
func WithClientUnaryClientInterceptors(unaryClientInterceptors ...grpc.UnaryClientInterceptor) ClientOption {
return func(o *ClientOptions) {
o.UnaryClientInterceptors = unaryClientInterceptors
}
}
func WithClientDiscovery(discovery resolver.Discovery) ClientOption {
return func(o *ClientOptions) {
o.discovery = discovery
}
}