forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
61 lines (47 loc) · 1.55 KB
/
middleware.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
package main
import "net/http"
import ()
type TykMiddlewareImplementation interface {
New()
GetConfig() (interface{}, error)
ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) // Handles request
}
func CreateDynamicMiddleware(MiddlewareName string, IsPre, UseSession bool, tykMwSuper TykMiddleware) func(http.Handler) http.Handler {
dMiddleware := &DynamicMiddleware{
TykMiddleware: tykMwSuper,
MiddlewareClassName: MiddlewareName,
Pre: IsPre,
UseSession: UseSession,
}
return CreateMiddleware(dMiddleware, tykMwSuper)
}
// Generic middleware caller to make extension easier
func CreateMiddleware(mw TykMiddlewareImplementation, tykMwSuper TykMiddleware) func(http.Handler) http.Handler {
aliceHandler := func(h http.Handler) http.Handler {
thisHandler := func(w http.ResponseWriter, r *http.Request) {
// construct a new instance
mw.New()
// Pull the configuration
thisMwConfiguration, confErr := mw.GetConfig()
if confErr != nil {
handler := ErrorHandler{tykMwSuper}
handler.HandleError(w, r, confErr.Error(), 403)
return
}
// Process the Request
reqErr, errCode := mw.ProcessRequest(w, r, thisMwConfiguration)
if reqErr != nil {
handler := ErrorHandler{tykMwSuper}
handler.HandleError(w, r, reqErr.Error(), errCode)
return
}
// Special code, bypasses all other execution
if errCode != 666 {
// No error, carry on...
h.ServeHTTP(w, r)
}
}
return http.HandlerFunc(thisHandler)
}
return aliceHandler
}