-
Notifications
You must be signed in to change notification settings - Fork 2
/
option.go
128 lines (109 loc) · 3.33 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
package micro
import (
"net/http"
"os"
"time"
"google.golang.org/grpc"
)
// Option - 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)
// Debug - return an Option to set the service to debug mode or not
func Debug(flag bool) Option {
return func(s *Service) {
s.debug = flag
}
}
// StaticDir - return an Option to set the staticDir
func StaticDir(staticDir string) Option {
return func(s *Service) {
s.staticDir = staticDir
}
}
// Redoc - return an Option to set the Redoc
func Redoc(redoc *RedocOpts) Option {
return func(s *Service) {
s.redoc = redoc
}
}
// Annotator - return an Option to append an annotator
func Annotator(annotator AnnotatorFunc) Option {
return func(s *Service) {
s.annotators = append(s.annotators, annotator)
}
}
// HTTPHandler - return an Option to set the httpHandler
func HTTPHandler(httpHandler HTTPHandlerFunc) Option {
return func(s *Service) {
s.httpHandler = httpHandler
}
}
// UnaryInterceptor - return an Option to append an unaryInterceptor
func UnaryInterceptor(unaryInterceptor grpc.UnaryServerInterceptor) Option {
return func(s *Service) {
s.unaryInterceptors = append(s.unaryInterceptors, unaryInterceptor)
}
}
// StreamInterceptor - return an Option to append an streamInterceptor
func StreamInterceptor(streamInterceptor grpc.StreamServerInterceptor) Option {
return func(s *Service) {
s.streamInterceptors = append(s.streamInterceptors, streamInterceptor)
}
}
// RouteOpt - return an Option to append a route
func RouteOpt(route Route) Option {
return func(s *Service) {
s.routes = append(s.routes, route)
}
}
// ShutdownFunc - return an Option to register a function which will be called when server shutdown
func ShutdownFunc(f func()) Option {
return func(s *Service) {
s.shutdownFunc = f
}
}
// ShutdownTimeout - return an Option to set the timeout before the server shutdown abruptly
func ShutdownTimeout(timeout time.Duration) Option {
return func(s *Service) {
s.shutdownTimeout = timeout
}
}
// PreShutdownDelay - return an Option to set the time waiting for running goroutines
// to finish their jobs before the shutdown starts
func PreShutdownDelay(timeout time.Duration) Option {
return func(s *Service) {
s.preShutdownDelay = timeout
}
}
// InterruptSignal - return an Option to append a interrupt signal
func InterruptSignal(signal os.Signal) Option {
return func(s *Service) {
s.interruptSignals = append(s.interruptSignals, signal)
}
}
// GRPCServerOption - return an Option to append a gRPC server option
func GRPCServerOption(serverOption grpc.ServerOption) Option {
return func(s *Service) {
s.grpcServerOptions = append(s.grpcServerOptions, serverOption)
}
}
// GRPCDialOption - return an Option to append a gRPC dial option
func GRPCDialOption(dialOption grpc.DialOption) Option {
return func(s *Service) {
s.grpcDialOptions = append(s.grpcDialOptions, dialOption)
}
}
// WithHTTPServer - return 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
}
}
func (s *Service) apply(opts ...Option) {
for _, opt := range opts {
opt(s)
}
}