Skip to content

Commit

Permalink
Another module moved to new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Buhr committed Aug 14, 2014
1 parent 7404850 commit 0416ff8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func getChain(spec APISpec) http.Handler {
tykMiddleware := TykMiddleware{spec, proxy}
chain := alice.New(
CreateMiddleware(&AuthKey{tykMiddleware}, tykMiddleware),
VersionCheck{tykMiddleware}.New(),
CreateMiddleware(&VersionCheck{tykMiddleware}, tykMiddleware),
CreateMiddleware(&KeyExpired{tykMiddleware}, tykMiddleware),
AccessRightsCheck{tykMiddleware}.New(),
RateLimitAndQuotaCheck{tykMiddleware}.New()).Then(proxyHandler)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func loadApps(APISpecs []APISpec, Muxer *http.ServeMux) {
chain := alice.New(
keyCheck,
CreateMiddleware(&KeyExpired{tykMiddleware}, tykMiddleware),
VersionCheck{tykMiddleware}.New(),
CreateMiddleware(&VersionCheck{tykMiddleware}, tykMiddleware),
AccessRightsCheck{tykMiddleware}.New(),
RateLimitAndQuotaCheck{tykMiddleware}.New()).Then(proxyHandler)

Expand Down
11 changes: 8 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ func CreateMiddleware(mw TykMiddlewareImplementation, tykMwSuper TykMiddleware)
}

// Process the Request
if reqErr, errCode := mw.ProcessRequest(w, r, thisMwConfiguration); reqErr != nil {
reqErr, errCode := mw.ProcessRequest(w, r, thisMwConfiguration)
if reqErr != nil {
handler := ErrorHandler{tykMwSuper}
handler.HandleError(w, r, reqErr.Error(), errCode)
return
}

// No error, carry on...
h.ServeHTTP(w, r)
// Special code, bypasses all other execution
if errCode != 666 {
// No error, carry on...
h.ServeHTTP(w, r)
}

}

return http.HandlerFunc(thisHandler)
Expand Down
52 changes: 24 additions & 28 deletions middleware_version_check.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import "errors"

import "net/http"

import ()
Expand All @@ -9,34 +11,28 @@ 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)
// New lets you do any initialisations for the object can be done here
func (v *VersionCheck) New() {}

// GetConfig retrieves the configuration from the API config
func (v *VersionCheck) GetConfig() (interface{}, error) {
return nil, nil
}

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (v *VersionCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
// Check versioning, blacklist, whitelist and ignored status
requestValid, stat := v.TykMiddleware.Spec.IsRequestValid(r)
if requestValid == false {
return errors.New(string(stat)), 409
}

if stat == StatusOkAndIgnore {
handler := SuccessHandler{v.TykMiddleware}
// Skip all other execution
handler.ServeHTTP(w, r)
return nil, 666
}

return aliceHandler
return nil, 200
}
2 changes: 1 addition & 1 deletion oauth_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func getOAuthChain(spec APISpec, Muxer *http.ServeMux) {
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, spec))
tykMiddleware := TykMiddleware{spec, proxy}
chain := alice.New(
VersionCheck{tykMiddleware}.New(),
CreateMiddleware(&VersionCheck{tykMiddleware}, tykMiddleware),
Oauth2KeyExists{tykMiddleware}.New(),
CreateMiddleware(&KeyExpired{tykMiddleware}, tykMiddleware),
AccessRightsCheck{tykMiddleware}.New(),
Expand Down

0 comments on commit 0416ff8

Please sign in to comment.