-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
177 lines (151 loc) · 4.67 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gmicro
import (
"net/http"
"os"
"time"
gRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)
// Option is service functional option
// See this post about the "functional options" pattern:
// http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
type Option func(s *Service)
func (s *Service) apply(opts []Option) {
for _, opt := range opts {
opt(s)
}
}
// WithRecovery service recover func.
func WithRecovery(f func()) Option {
return func(s *Service) {
s.recovery = f
}
}
// WithHTTPHandler returns an Option to set the httpHandler
func WithHTTPHandler(h HTTPHandlerFunc) Option {
return func(s *Service) {
s.httpHandler = h
}
}
// WithAnnotator returns an Option to append some annotator
func WithAnnotator(annotator ...AnnotatorFunc) Option {
return func(s *Service) {
s.annotators = append(s.annotators, annotator...)
}
}
// WithErrorHandler returns an Option to set the errorHandler
func WithErrorHandler(errorHandler gRuntime.ErrorHandlerFunc) Option {
return func(s *Service) {
s.errorHandler = errorHandler
}
}
// WithUnaryInterceptor returns an Option to append some unaryInterceptor
func WithUnaryInterceptor(unaryInterceptor ...grpc.UnaryServerInterceptor) Option {
return func(s *Service) {
s.unaryInterceptors = append(s.unaryInterceptors, unaryInterceptor...)
}
}
// WithStreamInterceptor returns an Option to append some streamInterceptor
func WithStreamInterceptor(streamInterceptor ...grpc.StreamServerInterceptor) Option {
return func(s *Service) {
s.streamInterceptors = append(s.streamInterceptors, streamInterceptor...)
}
}
// WithShutdownFunc returns an Option to register a function which will be called when server shutdown
func WithShutdownFunc(f func()) Option {
return func(s *Service) {
s.shutdownFunc = f
}
}
// WithShutdownTimeout returns an Option to set the timeout before the server shutdown abruptly
func WithShutdownTimeout(timeout time.Duration) Option {
return func(s *Service) {
s.shutdownTimeout = timeout
}
}
// WithPreShutdownDelay returns an Option to set the time waiting for running goroutines
// to finish their jobs before the shutdown starts
func WithPreShutdownDelay(timeout time.Duration) Option {
return func(s *Service) {
s.preShutdownDelay = timeout
}
}
// WithInterruptSignal returns an Option to append a interrupt signal
func WithInterruptSignal(signal os.Signal) Option {
return func(s *Service) {
s.interruptSignals = append(s.interruptSignals, signal)
}
}
// WithStaticDir returns an Option to set the staticDir
func WithStaticDir(dir string) Option {
return func(s *Service) {
s.staticDir = dir
}
}
// WithStaticAccess enable static file access
func WithStaticAccess(b bool) Option {
return func(s *Service) {
s.enableStaticAccess = b
}
}
// WithGRPCServerOption returns an Option to append a gRPC server option
func WithGRPCServerOption(serverOption ...grpc.ServerOption) Option {
return func(s *Service) {
s.gRPCServerOptions = append(s.gRPCServerOptions, serverOption...)
}
}
// WithGRPCDialOption returns an Option to append a gRPC dial option
func WithGRPCDialOption(dialOption ...grpc.DialOption) Option {
return func(s *Service) {
s.gRPCDialOptions = append(s.gRPCDialOptions, dialOption...)
}
}
// WithMuxOption returns an Option to append a mux option
func WithMuxOption(muxOption ...gRuntime.ServeMuxOption) Option {
return func(s *Service) {
s.muxOptions = append(s.muxOptions, muxOption...)
}
}
// WithHTTPServer returns an Option to set the http server, note that the Addr and Handler will be
// reset in startGRPCGateway(), so you are not able to specify them
func WithHTTPServer(server *http.Server) Option {
return func(s *Service) {
s.HTTPServer = server
}
}
// WithLogger uses the provided logger
func WithLogger(logger Logger) Option {
return func(s *Service) {
s.logger = logger
}
}
// WithRequestAccess request access log config.
func WithRequestAccess(b bool) Option {
return func(s *Service) {
s.enableRequestAccess = b
}
}
// WithPrometheus enble prometheus config.
func WithPrometheus(b bool) Option {
return func(s *Service) {
s.enablePrometheus = b
}
}
// WithHandlerFromEndpoint add handlerFromEndpoint to http gw endPoint
func WithHandlerFromEndpoint(reverseProxyFunc ...HandlerFromEndpoint) Option {
return func(s *Service) {
s.handlerFromEndpoints = append(s.handlerFromEndpoints, reverseProxyFunc...)
}
}
// WithRouteOpt adds additional routes
func WithRouteOpt(routes ...Route) Option {
return func(s *Service) {
s.routes = append(s.routes, routes...)
}
}
// WithGRPCNetwork set gRPC start network type.
func WithGRPCNetwork(network string) Option {
return func(s *Service) {
s.gRPCNetwork = network
}
}