This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
103 lines (85 loc) · 1.83 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
package gripkit
import (
"net/http"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"google.golang.org/grpc"
)
type HTTPOptions struct {
TLSCertPath string
TLSKeyPath string
Addr string
}
type HealthzOptions struct {
UseDefault bool
Handler http.HandlerFunc
// Optional IP:Port string, defaults to :10456
Addr string
}
type Option func(*options)
type options struct {
wrapGrpcWeb bool
grpcWebOptions []grpcweb.Option
httpOptions HTTPOptions
grpcOptions []grpc.ServerOption
wrapDebug bool
healthz *HealthzOptions
}
var (
defaultOptions = &options{
wrapGrpcWeb: false,
grpcWebOptions: []grpcweb.Option{},
grpcOptions: []grpc.ServerOption{},
wrapDebug: false,
httpOptions: HTTPOptions{
Addr: ":8080",
TLSKeyPath: "",
TLSCertPath: "",
},
healthz: &HealthzOptions{
UseDefault: true,
},
}
)
func evaluateOptions(optionList ...Option) *options {
evaluatedOptions := defaultOptions
for _, optionFunc := range optionList {
if optionFunc != nil {
optionFunc(evaluatedOptions)
}
}
return evaluatedOptions
}
func WithDebug() Option {
return func(o *options) {
o.wrapDebug = true
}
}
func WithGrpcWeb(opts ...grpcweb.Option) Option {
return func(o *options) {
o.wrapGrpcWeb = true
o.grpcWebOptions = opts
}
}
func WithOptions(opts ...grpc.ServerOption) Option {
return func(o *options) {
o.grpcOptions = opts
}
}
func WithHTTPOptions(opts HTTPOptions) Option {
return func(o *options) {
o.httpOptions = opts
}
}
// WithHealthz adds a custom /healthz handler to the gRPC HTTP server. Pass nil to disable.
func WithHealthz(hzOpts *HealthzOptions) Option {
return func(o *options) {
if hzOpts == nil {
o.healthz = nil
return
}
if hzOpts.Addr != "" {
o.healthz.Addr = hzOpts.Addr
}
o.healthz.Handler = hzOpts.Handler
}
}