forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_version_check.go
42 lines (32 loc) · 1.02 KB
/
middleware_version_check.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
package main
import "net/http"
import ()
// VersionCheck will check whether the version of the requested API the request is accessing has any restrictions on URL endpoints
type VersionCheck struct {
TykMiddleware
}
// New creates a new HttpHandler for the alice middleware package
func (s VersionCheck) New() func(http.Handler) http.Handler {
aliceHandler := func(h http.Handler) http.Handler {
thisHandler := func(w http.ResponseWriter, r *http.Request) {
// Check versioning, blacklist, whitelist and ignored status
requestValid, stat := s.TykMiddleware.Spec.IsRequestValid(r)
if requestValid == false {
handler := ErrorHandler{s.TykMiddleware}
// stop execution
handler.HandleError(w, r, string(stat), 409)
return
}
if stat == StatusOkAndIgnore {
handler := SuccessHandler{s.TykMiddleware}
// Skip all other execution
handler.ServeHTTP(w, r)
return
}
// Request is valid, carry on
h.ServeHTTP(w, r)
}
return http.HandlerFunc(thisHandler)
}
return aliceHandler
}