-
Notifications
You must be signed in to change notification settings - Fork 2
/
RequestChainHandler.go
118 lines (99 loc) · 4.58 KB
/
RequestChainHandler.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 goMiddlewareChain
import (
"context"
"net/http"
"github.com/julienschmidt/httprouter"
)
// RequestChainHandler chains all handler
func RequestChainHandler(responseHandler ResponseHandler, handlers ...Handler) httprouter.Handle {
return RequestChainHandlerWithResponseCheck(false, responseHandler, handlers...)
}
// RequestChainHandlerWithResponseCheck chains all handler and check every response
func RequestChainHandlerWithResponseCheck(checkResponseOfEveryHandler bool, responseHandler ResponseHandler, handlers ...Handler) httprouter.Handle {
return httprouter.Handle(func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
payload := Response{}
// iterate all handlers
for _, handler := range handlers {
handler(&payload, request, params)
if checkResponseOfEveryHandler && (payload.Status.Code != http.StatusOK && payload.Status.Code != 0) {
break
}
}
// pass responseHandler
responseHandler(&payload, writer, request, params)
})
}
// RequestChainContextHandler chains all handler
func RequestChainContextHandler(responseHandler ResponseHandler, handlers ...ContextHandler) httprouter.Handle {
return httprouter.Handle(func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
payload := Response{}
rootContext := context.Background()
// iterate all handlers
var runningContext context.Context
runningContext = rootContext
for _, handler := range handlers {
runningContext = handler(runningContext, &payload, request, params)
}
// pass responseHandler
responseHandler(&payload, writer, request, params)
})
}
// RestrictedRequestChainHandler need a RestrictHandler.
// A RestrictHandler returns bool if call is allowed.
func RestrictedRequestChainHandler(restrictHandler RestrictHandler, responseHandler ResponseHandler, handlers ...Handler) httprouter.Handle {
return RestrictedRequestChainHandlerWithResponseCheck(false, restrictHandler, responseHandler, handlers...)
}
// RestrictedRequestChainHandlerWithResponseCheck need a RestrictHandler.
// If checkResponseOfEveryHandler is true, handler check every response.
func RestrictedRequestChainHandlerWithResponseCheck(checkResponseOfEveryHandler bool, restrictHandler RestrictHandler, responseHandler ResponseHandler, handlers ...Handler) httprouter.Handle {
return httprouter.Handle(func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
payload := Response{}
// check restriction
allowed := restrictHandler(&payload, request, params)
if allowed {
// iterate all handlers
for _, handler := range handlers {
handler(&payload, request, params)
if checkResponseOfEveryHandler && (payload.Status.Code != http.StatusOK && payload.Status.Code != 0) {
break
}
}
} else if payload.Status.Code == 0 {
payload.Status.Code = http.StatusUnauthorized
payload.Status.Message = "failed by passing restrictHandler"
}
// pass ResponseHandler
responseHandler(&payload, writer, request, params)
})
}
// RestrictedRequestChainContextHandler need a RestrictHandler.
// A RestrictHandler returns bool if call is allowed.
func RestrictedRequestChainContextHandler(restrictHandler RestrictContextHandler, responseHandler ResponseHandler, handlers ...ContextHandler) httprouter.Handle {
return RestrictedRequestChainContextHandlerWithResponseCheck(false, restrictHandler, responseHandler, handlers...)
}
// RestrictedRequestChainContextHandlerWithResponseCheck exec all handlers
// If checkResponseOfEveryHandler is true, handler check every response.
func RestrictedRequestChainContextHandlerWithResponseCheck(checkResponseOfEveryHandler bool, restrictHandler RestrictContextHandler, responseHandler ResponseHandler, handlers ...ContextHandler) httprouter.Handle {
return httprouter.Handle(func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
payload := Response{}
rootContext := context.Background()
var runningContext context.Context
// check restriction
runningContext, allowed := restrictHandler(rootContext, &payload, request, params)
if allowed {
// iterate all handlers
runningContext = rootContext
for _, handler := range handlers {
runningContext = handler(runningContext, &payload, request, params)
if checkResponseOfEveryHandler && (payload.Status.Code != http.StatusOK && payload.Status.Code != 0) {
break
}
}
} else if payload.Status.Code == 0 {
payload.Status.Code = http.StatusUnauthorized
payload.Status.Message = "failed by passing restrictHandler"
}
// pass ResponseHandler
responseHandler(&payload, writer, request, params)
})
}