forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmw_track_endpoints.go
46 lines (36 loc) · 1.25 KB
/
mw_track_endpoints.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
package gateway
import (
"net/http"
"github.com/TykTechnologies/tyk/apidef"
)
// TrackEndpointMiddleware sets context variables to enable or disable whether Tyk should record analytitcs for a specific path.
type TrackEndpointMiddleware struct {
BaseMiddleware
}
func (t *TrackEndpointMiddleware) Name() string {
return "TrackEndpointMiddleware"
}
func (t *TrackEndpointMiddleware) EnabledForSpec() bool {
if !t.Spec.GlobalConfig.EnableAnalytics || t.Spec.DoNotTrack {
return false
}
for _, version := range t.Spec.VersionData.Versions {
if len(version.ExtendedPaths.TrackEndpoints) > 0 {
return true
}
}
return false
}
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (t *TrackEndpointMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
_, versionPaths, _, _ := t.Spec.Version(r)
foundTracked, metaTrack := t.Spec.CheckSpecMatchesStatus(r, versionPaths, RequestTracked)
if foundTracked {
ctxSetTrackedPath(r, metaTrack.(*apidef.TrackEndpointMeta).Path)
}
foundDnTrack, _ := t.Spec.CheckSpecMatchesStatus(r, versionPaths, RequestNotTracked)
if foundDnTrack {
ctxSetDoNotTrack(r, true)
}
return nil, http.StatusOK
}