-
Notifications
You must be signed in to change notification settings - Fork 29
/
service_router.go
511 lines (460 loc) · 16 KB
/
service_router.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package toolbox
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"reflect"
"strings"
"time"
)
const (
jsonContentType = "application/json"
yamlContentTypeSuffix = "/yaml"
textPlainContentType = "text/plain"
contentTypeHeader = "Content-Type"
)
const (
//MethodGet HTTP GET meothd
MethodGet = "GET"
MethodHead = "HEAD"
MethodPost = "POST"
MethodPut = "PUT"
MethodPatch = "PATCH" // RFC 5789
MethodDelete = "DELETE"
MethodOptions = "OPTIONS"
MethodTrace = "TRACE"
)
var httpMethods = map[string]bool{
MethodDelete: true,
MethodGet: true,
MethodPatch: true,
MethodPost: true,
MethodPut: true,
MethodHead: true,
MethodTrace: true,
MethodOptions: true,
}
//HandlerInvoker method is responsible of passing required parameters to router handler.
type HandlerInvoker func(serviceRouting *ServiceRouting, request *http.Request, response http.ResponseWriter, parameters map[string]interface{}) error
//DefaultEncoderFactory - NewJSONEncoderFactory
var DefaultEncoderFactory = NewJSONEncoderFactory()
//DefaultDecoderFactory - NewJSONDecoderFactory
var DefaultDecoderFactory = NewJSONDecoderFactory()
//YamlDefaultEncoderFactory - NewYamlEncoderFactory
var YamlDefaultEncoderFactory = NewYamlEncoderFactory()
//YamlDefaultDecoderFactory - NewYamlDecoderFactory
var YamlDefaultDecoderFactory = NewFlexYamlDecoderFactory()
//ServiceRouting represents a simple web services routing rule, which is matched with http request
type ServiceRouting struct {
URI string //matching uri
Handler interface{} //has to be func
HTTPMethod string
Parameters []string
ContentTypeEncoders map[string]EncoderFactory //content type encoder factory
ContentTypeDecoders map[string]DecoderFactory //content type decoder factory
HandlerInvoker HandlerInvoker //optional function that will be used instead of reflection to invoke a handler.
}
func (sr ServiceRouting) getDecoderFactory(contentType string) DecoderFactory {
if sr.ContentTypeDecoders != nil {
if factory, found := sr.ContentTypeDecoders[contentType]; found {
return factory
}
}
if strings.HasSuffix(contentType, yamlContentTypeSuffix) {
return YamlDefaultDecoderFactory
}
return DefaultDecoderFactory
}
func (sr ServiceRouting) getEncoderFactory(contentType string) EncoderFactory {
if sr.ContentTypeDecoders != nil {
if factory, found := sr.ContentTypeEncoders[contentType]; found {
return factory
}
}
if strings.HasSuffix(contentType, yamlContentTypeSuffix) {
return YamlDefaultEncoderFactory
}
return DefaultEncoderFactory
}
func (sr ServiceRouting) extractParameterFromBody(parameterName string, targetType reflect.Type, request *http.Request) (interface{}, error) {
targetValuePointer := reflect.New(targetType)
contentType := getContentTypeOrJSONContentType(request.Header.Get(contentTypeHeader))
decoderFactory := sr.getDecoderFactory(contentType)
body, err := ioutil.ReadAll(request.Body)
if err != nil {
return nil, err
}
decoder := decoderFactory.Create(bytes.NewReader(body))
if !strings.Contains(parameterName, ":") {
err := decoder.Decode(targetValuePointer.Interface())
if err != nil {
return nil, fmt.Errorf("unable to extract %T due to: %v, body: !%s!", targetValuePointer.Interface(), err, body)
}
} else {
var valueMap = make(map[string]interface{})
pair := strings.SplitN(parameterName, ":", 2)
valueMap[pair[1]] = targetValuePointer.Interface()
err := decoder.Decode(&valueMap)
if err != nil {
return nil, fmt.Errorf("unable to extract %T due to %v", targetValuePointer.Interface(), err)
}
}
return targetValuePointer.Interface(), nil
}
func (sr ServiceRouting) extractParameters(request *http.Request, response http.ResponseWriter) (map[string]interface{}, error) {
var result = make(map[string]interface{})
_ = request.ParseForm()
functionSignature := GetFuncSignature(sr.Handler)
uriParameters, _ := ExtractURIParameters(sr.URI, request.RequestURI)
for _, name := range sr.Parameters {
value, found := uriParameters[name]
if found {
if strings.Contains(value, ",") {
result[name] = strings.Split(value, ",")
} else {
result[name] = value
}
continue
}
value = request.Form.Get(name)
if len(value) > 0 {
result[name] = value
} else {
continue
}
}
if HasSliceAnyElements(sr.Parameters, "@httpRequest") {
result["@httpRequest"] = request
}
if HasSliceAnyElements(sr.Parameters, "@httpResponseWriter") {
result["@httpResponseWriter"] = response
}
if request.ContentLength > 0 {
for i, parameter := range sr.Parameters {
if _, found := result[parameter]; !found {
value, err := sr.extractParameterFromBody(parameter, functionSignature[i], request)
if err != nil {
return nil, fmt.Errorf("failed to extract parameters for %v %v due to %v", sr.HTTPMethod, sr.URI, err)
}
result[parameter] = value
break
}
}
}
return result, nil
}
//ServiceRouter represents routing rule
type ServiceRouter struct {
serviceRouting []*ServiceRouting
}
func (r *ServiceRouter) match(request *http.Request) []*ServiceRouting {
var result = make([]*ServiceRouting, 0)
for _, candidate := range r.serviceRouting {
if candidate.HTTPMethod == request.Method {
_, matched := ExtractURIParameters(candidate.URI, request.RequestURI)
if matched {
result = append(result, candidate)
}
}
}
return result
}
func getContentTypeOrJSONContentType(contentType string) string {
if strings.Contains(contentType, textPlainContentType) || strings.Contains(contentType, jsonContentType) || contentType == "" {
return jsonContentType
}
return contentType
}
//Route matches service routing by http method , and number of parameters, then it call routing method, and sent back its response.
func (r *ServiceRouter) Route(response http.ResponseWriter, request *http.Request) error {
candidates := r.match(request)
if len(candidates) == 0 {
var uriTemplates = make([]string, 0)
for _, routing := range r.serviceRouting {
uriTemplates = append(uriTemplates, routing.URI)
}
return fmt.Errorf("failed to route request - unable to match %v with one of %v", request.RequestURI, strings.Join(uriTemplates, ","))
}
var finalError error
for _, serviceRouting := range candidates {
parameterValues, err := serviceRouting.extractParameters(request, response)
if err != nil {
finalError = fmt.Errorf("unable to extract parameters due to %v", err)
continue
}
if serviceRouting.HandlerInvoker != nil {
err := serviceRouting.HandlerInvoker(serviceRouting, request, response, parameterValues)
if err != nil {
finalError = fmt.Errorf("unable to extract parameters due to %v", err)
}
continue
}
functionParameters, err := BuildFunctionParameters(serviceRouting.Handler, serviceRouting.Parameters, parameterValues)
if err != nil {
finalError = fmt.Errorf("unable to build function parameters %T due to %v", serviceRouting.Handler, err)
continue
}
result := CallFunction(serviceRouting.Handler, functionParameters...)
if len(result) > 0 {
err = WriteServiceRoutingResponse(response, request, serviceRouting, result[0])
if err != nil {
return fmt.Errorf("failed to write response response %v, due to %v", result[0], err)
}
return nil
}
if response.Header().Get(contentTypeHeader) == "" {
response.Header().Set(contentTypeHeader, textPlainContentType)
}
}
if finalError != nil {
return fmt.Errorf("failed to route request - %v", finalError)
}
return nil
}
//WriteServiceRoutingResponse writes service router response
func WriteServiceRoutingResponse(response http.ResponseWriter, request *http.Request, serviceRouting *ServiceRouting, result interface{}) error {
if result == nil {
result = struct{}{}
}
statusCodeAccessor, ok := result.(StatucCodeAccessor)
if ok {
statusCode := statusCodeAccessor.GetStatusCode()
if statusCode > 0 && statusCode != http.StatusOK {
response.WriteHeader(statusCode)
return nil
}
}
contentTypeAccessor, ok := result.(ContentTypeAccessor)
var responseContentType string
if ok {
responseContentType = contentTypeAccessor.GetContentType()
}
if responseContentType == "" {
requestContentType := request.Header.Get(contentTypeHeader)
responseContentType = getContentTypeOrJSONContentType(requestContentType)
}
encoderFactory := serviceRouting.getEncoderFactory(responseContentType)
encoder := encoderFactory.Create(response)
if response.Header().Get(contentTypeHeader) == "" {
response.Header().Set(contentTypeHeader, responseContentType)
}
err := encoder.Encode(result)
if err != nil {
return fmt.Errorf("failed to encode response %v, due to %v", response, err)
}
return nil
}
//WriteResponse writes response to response writer, it used encoder factory to encode passed in response to the writer, it sets back request contenttype to response.
func (r *ServiceRouter) WriteResponse(encoderFactory EncoderFactory, response interface{}, request *http.Request, responseWriter http.ResponseWriter) error {
requestContentType := request.Header.Get(contentTypeHeader)
responseContentType := getContentTypeOrJSONContentType(requestContentType)
encoder := encoderFactory.Create(responseWriter)
if responseWriter.Header().Get(contentTypeHeader) == "" {
responseWriter.Header().Set(contentTypeHeader, responseContentType)
}
err := encoder.Encode(response)
if err != nil {
return fmt.Errorf("failed to encode response %v, due to %v", response, err)
}
return nil
}
//NewServiceRouter creates a new service router, is takes list of service routing as arguments
func NewServiceRouter(serviceRouting ...ServiceRouting) *ServiceRouter {
var routings = make([]*ServiceRouting, 0)
for i := range serviceRouting {
routings = append(routings, &serviceRouting[i])
}
return &ServiceRouter{routings}
}
//RouteToService calls web service url, with passed in json request, and encodes http json response into passed response
func RouteToService(method, url string, request, response interface{}, options ...*HttpOptions) (err error) {
client, err := NewToolboxHTTPClient(options...)
if err != nil {
return err
}
return client.Request(method, url, request, response, NewJSONEncoderFactory(), NewJSONDecoderFactory())
}
type HttpOptions struct {
Key string
Value interface{}
}
func NewHttpClient(options ...*HttpOptions) (*http.Client, error) {
if len(options) == 0 {
return http.DefaultClient, nil
}
var (
// Default values matching DefaultHttpClient
RequestTimeoutMs = 30 * time.Second
KeepAliveTimeMs = 30 * time.Second
TLSHandshakeTimeoutMs = 10 * time.Second
ExpectContinueTimeout = 1 * time.Second
IdleConnTimeout = 90 * time.Second
DualStack = true
MaxIdleConnsPerHost = http.DefaultMaxIdleConnsPerHost
MaxIdleConns = 100
FollowRedirects = true
ResponseHeaderTimeoutMs time.Duration
TimeoutMs time.Duration
)
for _, option := range options {
switch option.Key {
case "RequestTimeoutMs":
RequestTimeoutMs = time.Duration(AsInt(option.Value)) * time.Millisecond
case "TimeoutMs":
TimeoutMs = time.Duration(AsInt(option.Value)) * time.Millisecond
case "KeepAliveTimeMs":
KeepAliveTimeMs = time.Duration(AsInt(option.Value)) * time.Millisecond
case "TLSHandshakeTimeoutMs":
KeepAliveTimeMs = time.Duration(AsInt(option.Value)) * time.Millisecond
case "ResponseHeaderTimeoutMs":
ResponseHeaderTimeoutMs = time.Duration(AsInt(option.Value)) * time.Millisecond
case "MaxIdleConns":
MaxIdleConns = AsInt(option.Value)
case "MaxIdleConnsPerHost":
MaxIdleConnsPerHost = AsInt(option.Value)
case "DualStack":
DualStack = AsBoolean(option.Value)
case "FollowRedirects":
FollowRedirects = AsBoolean(option.Value)
default:
return nil, fmt.Errorf("Invalid option: %v", option.Key)
}
}
roundTripper := http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: RequestTimeoutMs,
KeepAlive: KeepAliveTimeMs,
DualStack: DualStack,
}).DialContext,
MaxIdleConns: MaxIdleConns,
ExpectContinueTimeout: ExpectContinueTimeout,
IdleConnTimeout: IdleConnTimeout,
TLSHandshakeTimeout: TLSHandshakeTimeoutMs,
MaxIdleConnsPerHost: MaxIdleConnsPerHost,
ResponseHeaderTimeout: ResponseHeaderTimeoutMs,
}
client := &http.Client{
Transport: &roundTripper,
Timeout: TimeoutMs,
}
if !FollowRedirects {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
return client, nil
}
// ToolboxHTTPClient contains preconfigured http client
type ToolboxHTTPClient struct {
httpClient *http.Client
}
// NewToolboxHTTPClient instantiate new client with provided options
func NewToolboxHTTPClient(options ...*HttpOptions) (*ToolboxHTTPClient, error) {
client, err := NewHttpClient(options...)
if err != nil {
return nil, err
}
return &ToolboxHTTPClient{client}, nil
}
// Request sends http request using the existing client
func (c *ToolboxHTTPClient) Request(method, url string, request, response interface{}, encoderFactory EncoderFactory, decoderFactory DecoderFactory) (err error) {
if _, found := httpMethods[strings.ToUpper(method)]; !found {
return errors.New("unsupported method:" + method)
}
var buffer *bytes.Buffer
if request != nil {
buffer = new(bytes.Buffer)
if IsString(request) {
buffer.Write([]byte(AsString(request)))
} else {
err := encoderFactory.Create(buffer).Encode(&request)
if err != nil {
return fmt.Errorf("failed to encode request: %v due to ", err)
}
}
}
var serverResponse *http.Response
var httpRequest *http.Request
httpMethod := strings.ToUpper(method)
if request != nil {
httpRequest, err = http.NewRequest(httpMethod, url, buffer)
if err != nil {
return err
}
httpRequest.Header.Set(contentTypeHeader, jsonContentType)
} else {
httpRequest, err = http.NewRequest(httpMethod, url, nil)
if err != nil {
return err
}
}
serverResponse, err = c.httpClient.Do(httpRequest)
if serverResponse != nil {
// must close we have serverResponse to avoid fd leak
defer serverResponse.Body.Close()
}
if err != nil && serverResponse != nil {
return fmt.Errorf("failed to get response %v %v", err, serverResponse.Header.Get("error"))
}
if response != nil {
updateResponse(serverResponse, response)
if serverResponse == nil {
return fmt.Errorf("failed to receive response %v", err)
}
var errorPrefix = fmt.Sprintf("failed to process response: %v, ", serverResponse.StatusCode)
body, err := ioutil.ReadAll(serverResponse.Body)
if err != nil {
return fmt.Errorf("%v unable read body %v", errorPrefix, err)
}
if len(body) == 0 {
return fmt.Errorf("%v response body was empty", errorPrefix)
}
if serverResponse.StatusCode == http.StatusNotFound {
updateResponse(serverResponse, response)
return nil
}
if int(serverResponse.StatusCode/100)*100 == http.StatusInternalServerError {
return errors.New(string(body))
}
err = decoderFactory.Create(strings.NewReader(string(body))).Decode(response)
if err != nil {
return fmt.Errorf("%v. unable decode response as %T: body: %v: %v", errorPrefix, response, string(body), err)
}
updateResponse(serverResponse, response)
}
return nil
}
//StatucCodeMutator client side reponse optional interface
type StatucCodeMutator interface {
SetStatusCode(code int)
}
//StatucCodeAccessor server side response accessor
type StatucCodeAccessor interface {
GetStatusCode() int
}
//ContentTypeMutator client side reponse optional interface
type ContentTypeMutator interface {
SetContentType(contentType string)
}
//ContentTypeAccessor server side response accessor
type ContentTypeAccessor interface {
GetContentType() string
}
//updateResponse update response with content type and status code if applicable
func updateResponse(httpResponse *http.Response, response interface{}) {
if response == nil {
return
}
statusCodeMutator, ok := response.(StatucCodeMutator)
if ok {
statusCodeMutator.SetStatusCode(httpResponse.StatusCode)
}
contentTypeMutator, ok := response.(ContentTypeMutator)
if ok {
contentTypeMutator.SetContentType(httpResponse.Header.Get(contentTypeHeader))
}
}