-
Notifications
You must be signed in to change notification settings - Fork 0
/
Executor.go
66 lines (61 loc) · 1.81 KB
/
Executor.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
package goose
/**
* Sahil Gulati {sahil.gulati1991@outlook.com}
* This file contains functionality of execution of middlewares.
**/
import (
"net/http"
)
type GooseMiddlewareExecutor struct {
toProcess bool
route *GooseRoute
message *GooseMessage
request *http.Request
response *GooseResponse
}
func (gME GooseMiddlewareExecutor) GetInstance() *GooseMiddlewareExecutor {
gooseMiddlewareExecutor := new(GooseMiddlewareExecutor)
gooseMiddlewareExecutor.route = gME.route
gooseMiddlewareExecutor.message = gME.message
gooseMiddlewareExecutor.request = gME.request
gooseMiddlewareExecutor.response = gME.response
gooseMiddlewareExecutor.toProcess = gME.toProcess
return gooseMiddlewareExecutor
}
/**
* This function will be response for execution of middleware.
*/
func (gME *GooseMiddlewareExecutor) Execute() bool {
if gME.toProcess {
for _, middleware := range gME.route.middlewares {
proceed, response := middleware(gME.request, gME.message)
if !proceed {
gME.toProcess = proceed
gME.response.Headers = response.Headers
gME.response.Response = response.Response
gME.response.StatusCode = response.StatusCode
return false
}
}
}
return true
}
/**
* This function will be responsible for execution of final action.
*/
func (gME *GooseMiddlewareExecutor) Action() {
if gME.toProcess {
response := gME.route.endpoint(gME.request, gME.message)
if value, asserted := response.(GooseResponse); asserted {
gME.response.Headers = value.Headers
gME.response.Response = value.Response
gME.response.StatusCode = value.StatusCode
} else if value, asserted := response.(*GooseResponse); asserted {
gME.response.Headers = value.Headers
gME.response.Response = value.Response
gME.response.StatusCode = value.StatusCode
} else {
gME.response.Response = response
}
}
}