-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
80 lines (65 loc) · 1.9 KB
/
config.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
package grpcserver
import (
"github.com/boxgo/box/pkg/config"
"github.com/boxgo/box/pkg/logger"
"google.golang.org/grpc"
)
type (
Config struct {
path string
wrap Wrap
serverOptions []grpc.ServerOption
unaryServerInterceptor []grpc.UnaryServerInterceptor
streamServerInterceptor []grpc.StreamServerInterceptor
Network string `config:"network" desc:"The network must be \"tcp\", \"tcp4\", \"tcp6\", \"unix\" or \"unixpacket\""`
Addr string `config:"addr" desc:"format: host:port"`
Reflection bool `config:"reflection" desc:"Enable server reflection service"`
}
Wrap func(*grpc.Server)
OptionFunc func(*Config)
)
func WithWrap(wrap Wrap) OptionFunc {
return func(c *Config) {
c.wrap = wrap
}
}
func WithServerOption(opt ...grpc.ServerOption) OptionFunc {
return func(c *Config) {
c.serverOptions = append(c.serverOptions, opt...)
}
}
func WithUnaryServerInterceptor(interceptor ...grpc.UnaryServerInterceptor) OptionFunc {
return func(c *Config) {
c.unaryServerInterceptor = append(c.unaryServerInterceptor, interceptor...)
}
}
func WithStreamServerInterceptor(interceptor ...grpc.StreamServerInterceptor) OptionFunc {
return func(c *Config) {
c.streamServerInterceptor = append(c.streamServerInterceptor, interceptor...)
}
}
func StdConfig(key string, optionFunc ...OptionFunc) *Config {
cfg := DefaultConfig(key)
for _, fn := range optionFunc {
fn(cfg)
}
if err := config.Scan(cfg); err != nil {
logger.Panicf("gRPC server build error: %s", err)
}
return cfg
}
func DefaultConfig(key string) *Config {
return &Config{
path: "grpc_server." + key,
wrap: func(server *grpc.Server) {},
Network: "tcp4",
Addr: ":9001",
Reflection: false,
}
}
func (c *Config) Path() string {
return c.path
}
func (c *Config) Build() *Server {
return newGRpcServer(c)
}