-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gateway.go
118 lines (106 loc) · 2.59 KB
/
Gateway.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
package goose
import (
// "fmt"
"net/http"
"time"
)
type GooseGateway struct {
routes map[string]*GooseRoute
regexRoutes map[string]*GooseRoute
route *GooseRoute
holder interface{}
}
/**
* This function will be responsible for:
* @1 Getting default variables
* @1 Execution of middlewares
* @2 Execution of final endpoint
* @3 Preparing and emitting final response.
*/
func (gateway GooseGateway) Pipeline(w http.ResponseWriter, request *http.Request) {
/** @1 **/
message, defaultResponse := gateway.getDefaults(request)
/** @2 **/
toProcess := GooseValidator{
route: gateway.route,
request: request,
response: defaultResponse,
}.GetInstance().validate()
/** @3 **/
toProcess = GooseMiddlewareExecutor{
toProcess: toProcess,
route: gateway.route,
request: request,
message: message,
response: defaultResponse,
}.GetInstance().Execute()
/** @4 **/
GooseMiddlewareExecutor{
toProcess: toProcess,
route: gateway.route,
request: request,
message: message,
response: defaultResponse,
}.GetInstance().Action()
/** @5 **/
GooseResponder{
writer: w,
route: gateway.route,
response: defaultResponse,
}.GetInstance().Respond()
}
func (gateway GooseGateway) RegexPipeline(w http.ResponseWriter, request *http.Request) {
/** @1 **/
message, defaultResponse := gateway.getDefaults(request)
/** @2 **/
toProcess, route := GooseValidator{
regexRoutes: gateway.regexRoutes,
request: request,
message: message,
response: defaultResponse,
}.GetInstance().regexValidate()
/** @3 **/
toProcess = GooseMiddlewareExecutor{
toProcess: toProcess,
route: route,
request: request,
message: message,
response: defaultResponse,
}.GetInstance().Execute()
/** @4 **/
GooseMiddlewareExecutor{
toProcess: toProcess,
route: route,
request: request,
message: message,
response: defaultResponse,
}.GetInstance().Action()
/** @5 **/
GooseResponder{
writer: w,
route: route,
response: defaultResponse,
}.GetInstance().Respond()
}
/**
* This function will create a new gooseMessage it
*@1 Adding requestId
*@2 Adding requestTime
*@3 Previous provided user holder
*/
func (gateway GooseGateway) getDefaults(request *http.Request) (*GooseMessage, *GooseResponse) {
requestTime := time.Now().UnixNano()
return &GooseMessage{
RequestId: requestTime,
RequestTime: requestTime,
Holder: gateway.holder,
RequestHeaders: getHeaders(request),
GetParams: getParams(request),
},
&GooseResponse{
Headers: map[string]string{
CONTENT_TYPE: PLAIN_TEXT,
},
StatusCode: 200,
}
}