-
Notifications
You must be signed in to change notification settings - Fork 6
/
option.go
230 lines (195 loc) · 4.81 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package httpx
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"time"
"github.com/ml444/gkit/middleware"
)
// ServerOption is an HTTP server option.
type ServerOption func(*Server)
// Network with server network.
func Network(network string) ServerOption {
return func(s *Server) {
s.network = network
}
}
// Address with server address.
func Address(addr string) ServerOption {
return func(s *Server) {
s.address = addr
}
}
// Endpoint with server address.
func Endpoint(endpoint *url.URL) ServerOption {
return func(s *Server) {
s.endpoint = endpoint
}
}
// Timeout with server timeout.
func Timeout(timeout time.Duration) ServerOption {
return func(s *Server) {
s.timeout = timeout
}
}
// TLSConfig with TLS config.
func TLSConfig(c *tls.Config) ServerOption {
return func(o *Server) {
o.tlsConf = c
}
}
// Listener with server listener.
func Listener(lis net.Listener) ServerOption {
return func(s *Server) {
s.listener = lis
}
}
// SetMiddlewares with server middlewares.
func SetMiddlewares(mws ...middleware.Middleware) ServerOption {
return func(s *Server) {
s.middlewares = append(s.middlewares, mws...)
}
}
// RouterPathPrefix with mux's PathPrefix, coder will replace by a subrouter that start with RootPrefix.
func RouterPathPrefix(prefix string) ServerOption {
return func(s *Server) {
s.routerCfg.RootPrefix = prefix
}
}
// RouterStrictSlash with mux's StrictSlash
// If true, when the path pattern is "/path/", accessing "/path" will
// redirect to the former and vice versa.
func RouterStrictSlash(strictSlash bool) ServerOption {
return func(s *Server) {
s.routerCfg.StrictSlash = strictSlash
}
}
// RouterSkipClean with mux's SkipClean
func RouterSkipClean(skipClean bool) ServerOption {
return func(s *Server) {
s.routerCfg.SkipClean = skipClean
}
}
// RouterUseEncodedPath with mux's SkipClean
func RouterUseEncodedPath() ServerOption {
return func(s *Server) {
s.routerCfg.UseEncodedPath = true
}
}
// RouterNotFoundHandler
func RouterNotFoundHandler(handler http.Handler) ServerOption {
return func(s *Server) {
s.routerCfg.NotFoundHandler = handler
}
}
// RouterMethodNotAllowedHandler
func RouterMethodNotAllowedHandler(handler http.Handler) ServerOption {
return func(s *Server) {
s.routerCfg.MethodNotAllowedHandler = handler
}
}
// RouterCoder
func RouterCoder(coder IRouterCoder) ServerOption {
return func(s *Server) {
s.routerCfg.Coder = coder
}
}
// ClientOption is HTTP client option.
type ClientOption func(*Client)
// WithTransport with client transport.
func WithTransport(trans http.RoundTripper) ClientOption {
return func(o *Client) {
o.transport = trans
}
}
// WithTimeout with client request timeout.
func WithTimeout(d time.Duration) ClientOption {
return func(o *Client) {
o.timeout = d
}
}
// WithUserAgent with client user agent.
func WithUserAgent(ua string) ClientOption {
return func(o *Client) {
o.userAgent = ua
}
}
// WithMiddlewares with client middleware.
func WithMiddlewares(m ...middleware.Middleware) ClientOption {
return func(o *Client) {
o.middleware = m
}
}
// WithEndpoint with client addr.
func WithEndpoint(endpoint string) ClientOption {
return func(o *Client) {
o.endpoint = endpoint
}
}
// WithRequestEncoder with client request encoder.
func WithRequestEncoder(encoder EncodeRequestFunc) ClientOption {
return func(o *Client) {
o.encoder = encoder
}
}
// WithResponseDecoder with client response decoder.
func WithResponseDecoder(decoder DecodeResponseFunc) ClientOption {
return func(o *Client) {
o.decoder = decoder
}
}
// WithTLSConfig with tls config.
func WithTLSConfig(c *tls.Config) ClientOption {
return func(o *Client) {
o.tlsConf = c
}
}
type CallOption func(*callInfo)
type callInfo struct {
reqHeader http.Header
operation string
pathTemplate string
onResponse func(*http.Response) error
}
func defaultCallInfo(path string) callInfo {
return callInfo{
reqHeader: http.Header{
"Content-Type": []string{"application/json"},
},
operation: path,
pathTemplate: path,
}
}
// SetRequestContentType with request content type.
func SetRequestContentType(contentType string) CallOption {
return func(info *callInfo) {
info.reqHeader.Set("Content-Type", contentType)
}
}
func RequestHeader(header http.Header) CallOption {
return func(ci *callInfo) {
ci.reqHeader = header
}
}
func AddRequestHeader(key, value string) CallOption {
return func(ci *callInfo) {
ci.reqHeader.Add(key, value)
}
}
func Operation(operation string) CallOption {
return func(info *callInfo) {
info.operation = operation
}
}
// PathTemplate is http path template
func PathTemplate(pattern string) CallOption {
return func(info *callInfo) {
info.pathTemplate = pattern
}
}
func OnResponse(onResponse func(*http.Response) error) CallOption {
return func(info *callInfo) {
info.onResponse = onResponse
}
}