forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
113 lines (94 loc) · 3.18 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
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
package main
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/gocraft/health"
"github.com/paulbellamy/ratecounter"
)
var GlobalRate = ratecounter.NewRateCounter(1 * time.Second)
type TykMiddlewareImplementation interface {
New()
GetConfig() (interface{}, error)
ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) // Handles request
IsEnabledForSpec() bool
GetName() string
}
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)
}
func CreateDynamicAuthMiddleware(MiddlewareName string, tykMwSuper *TykMiddleware) func(http.Handler) http.Handler {
dMiddleware := &DynamicMiddleware{
TykMiddleware: tykMwSuper,
MiddlewareClassName: MiddlewareName,
Auth: true,
UseSession: false,
}
return CreateMiddleware(dMiddleware, tykMwSuper)
}
// Generic middleware caller to make extension easier
func CreateMiddleware(mw TykMiddlewareImplementation, tykMwSuper *TykMiddleware) func(http.Handler) http.Handler {
// construct a new instance
mw.New()
// Pull the configuration
mwConf, confErr := mw.GetConfig()
if confErr != nil {
log.Fatal("[Middleware] Configuration load failed")
}
aliceHandler := func(h http.Handler) http.Handler {
handler := func(w http.ResponseWriter, r *http.Request) {
job := instrument.NewJob("MiddlewareCall")
meta := health.Kvs{
"from_ip": fmt.Sprint(r.RemoteAddr),
"method": r.Method,
"endpoint": r.URL.Path,
"raw_url": r.URL.String(),
"size": strconv.Itoa(int(r.ContentLength)),
"mw_name": mw.GetName(),
}
eventName := mw.GetName() + "." + "executed"
job.EventKv("executed", meta)
job.EventKv(eventName, meta)
startTime := time.Now()
if tykMwSuper.Spec.CORS.OptionsPassthrough && r.Method == "OPTIONS" {
h.ServeHTTP(w, r)
} else {
reqErr, errCode := mw.ProcessRequest(w, r, mwConf)
if reqErr != nil {
handler := ErrorHandler{tykMwSuper}
handler.HandleError(w, r, reqErr.Error(), errCode)
meta["error"] = reqErr.Error()
job.TimingKv("exec_time", time.Since(startTime).Nanoseconds(), meta)
job.TimingKv(eventName+".exec_time", time.Since(startTime).Nanoseconds(), meta)
return
}
// Special code, stops execution
if errCode == 1666 {
// Stop
log.Info("[Middleware] Received stop code")
meta["stopped"] = "1"
job.TimingKv("exec_time", time.Since(startTime).Nanoseconds(), meta)
job.TimingKv(eventName+".exec_time", time.Since(startTime).Nanoseconds(), meta)
return
}
// Special code, bypasses all other execution
if errCode != 666 {
// No error, carry on...
meta["bypass"] = "1"
h.ServeHTTP(w, r)
}
job.TimingKv("exec_time", time.Since(startTime).Nanoseconds(), meta)
job.TimingKv(eventName+".exec_time", time.Since(startTime).Nanoseconds(), meta)
}
}
return http.HandlerFunc(handler)
}
return aliceHandler
}