Skip to content

Commit

Permalink
Made middleware lighter, now using pointers throughout so there is le…
Browse files Browse the repository at this point in the history
…ss object copying
  • Loading branch information
lonelycode committed Jul 7, 2015
1 parent d1701e2 commit 34cce45
Show file tree
Hide file tree
Showing 26 changed files with 40 additions and 42 deletions.
4 changes: 2 additions & 2 deletions batch_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func getBatchTestChain(spec APISpec) http.Handler {
spec.Init(&redisStore, &redisStore, healthStore, orgStore)
remote, _ := url.Parse("http://httpbin.org/")
proxy := TykNewSingleHostReverseProxy(remote, &spec)
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, spec))
tykMiddleware := TykMiddleware{spec, proxy}
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, &spec))
tykMiddleware := &TykMiddleware{&spec, proxy}
chain := alice.New(
CreateMiddleware(&IPWhiteListMiddleware{TykMiddleware: tykMiddleware}, tykMiddleware),
CreateMiddleware(&AuthKey{tykMiddleware}, tykMiddleware),
Expand Down
6 changes: 3 additions & 3 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ func getChain(spec APISpec) http.Handler {
spec.Init(&redisStore, &redisStore, healthStore, orgStore)
remote, _ := url.Parse("http://lonelycode.com/")
proxy := TykNewSingleHostReverseProxy(remote, &spec)
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, spec))
tykMiddleware := TykMiddleware{spec, proxy}
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, &spec))
tykMiddleware := &TykMiddleware{&spec, proxy}
chain := alice.New(
CreateMiddleware(&IPWhiteListMiddleware{tykMiddleware}, tykMiddleware),
CreateMiddleware(&AuthKey{tykMiddleware}, tykMiddleware),
CreateMiddleware(&VersionCheck{TykMiddleware:tykMiddleware}, tykMiddleware),
CreateMiddleware(&VersionCheck{TykMiddleware: tykMiddleware}, tykMiddleware),
CreateMiddleware(&KeyExpired{tykMiddleware}, tykMiddleware),
CreateMiddleware(&AccessRightsCheck{tykMiddleware}, tykMiddleware),
CreateMiddleware(&RateLimitAndQuotaCheck{tykMiddleware}, tykMiddleware)).Then(proxyHandler)
Expand Down
2 changes: 1 addition & 1 deletion handler_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type APIError struct {
// ErrorHandler is invoked whenever there is an issue with a proxied request, most middleware will invoke
// the ErrorHandler if something is wrong with the request and halt the request processing through the chain
type ErrorHandler struct {
TykMiddleware
*TykMiddleware
}

// HandleError is the actual error handler and will store the error details in analytics if analytics processing is enabled.
Expand Down
4 changes: 2 additions & 2 deletions handler_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ func (d DummyProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

// ProxyHandler Proxies requests through to their final destination, if they make it through the middleware chain.
func ProxyHandler(p *ReverseProxy, apiSpec APISpec) func(http.ResponseWriter, *http.Request) {
func ProxyHandler(p *ReverseProxy, apiSpec *APISpec) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {

tm := TykMiddleware{apiSpec, p}
handler := SuccessHandler{tm}
handler := SuccessHandler{&tm}
// Skip all other execution
handler.ServeHTTP(w, r)
return
Expand Down
4 changes: 2 additions & 2 deletions handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
// TykMiddleware wraps up the ApiSpec and Proxy objects to be included in a
// middleware handler, this can probably be handled better.
type TykMiddleware struct {
Spec APISpec
Spec *APISpec
Proxy *ReverseProxy
}

Expand Down Expand Up @@ -102,7 +102,7 @@ func (t TykMiddleware) CheckSessionAndIdentityForValidKey(key string) (SessionSt

// SuccessHandler represents the final ServeHTTP() request for a proxied API request
type SuccessHandler struct {
TykMiddleware
*TykMiddleware
}

func (s SuccessHandler) RecordHit(w http.ResponseWriter, r *http.Request, timing int64) {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func loadApps(APISpecs []APISpec, Muxer *http.ServeMux) {
creeateResponseMiddlewareChain(&referenceSpec)

//proxyHandler := http.HandlerFunc(ProxyHandler(proxy, referenceSpec))
tykMiddleware := TykMiddleware{referenceSpec, proxy}
tykMiddleware := &TykMiddleware{&referenceSpec, proxy}

keyPrefix := "cache-" + referenceSpec.APIDefinition.APIID
CacheStore := &RedisStorageManager{KeyPrefix: keyPrefix}
Expand Down
4 changes: 2 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type TykMiddlewareImplementation interface {
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 {
func CreateDynamicMiddleware(MiddlewareName string, IsPre, UseSession bool, tykMwSuper *TykMiddleware) func(http.Handler) http.Handler {
dMiddleware := &DynamicMiddleware{
TykMiddleware: tykMwSuper,
MiddlewareClassName: MiddlewareName,
Expand All @@ -22,7 +22,7 @@ func CreateDynamicMiddleware(MiddlewareName string, IsPre, UseSession bool, tykM
}

// Generic middleware caller to make extension easier
func CreateMiddleware(mw TykMiddlewareImplementation, tykMwSuper TykMiddleware) func(http.Handler) http.Handler {
func CreateMiddleware(mw TykMiddlewareImplementation, tykMwSuper *TykMiddleware) func(http.Handler) http.Handler {
// construct a new instance
mw.New()

Expand Down
2 changes: 1 addition & 1 deletion middleware_access_rights.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// permission to access the specific version. If no permission data is in the SessionState, then
// it is assumed that the user can go through.
type AccessRightsCheck struct {
TykMiddleware
*TykMiddleware
}

// New lets you do any initialisations for the object can be done here
Expand Down
4 changes: 2 additions & 2 deletions middleware_auth_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// KeyExists will check if the key being used to access the API is in the request data,
// and then if the key is in the storage engine
type AuthKey struct {
TykMiddleware
*TykMiddleware
}

func (k AuthKey) New() {}
Expand Down Expand Up @@ -64,7 +64,7 @@ func (k *AuthKey) ProcessRequest(w http.ResponseWriter, r *http.Request, configu
return nil, 200
}

func AuthFailed(m TykMiddleware, r *http.Request, authHeaderValue string) {
func AuthFailed(m *TykMiddleware, r *http.Request, authHeaderValue string) {
go m.FireEvent(EVENT_AuthFailure,
EVENT_AuthFailureMeta{
EventMetaDefault: EventMetaDefault{Message: "Auth Failure", OriginatingRequest: EncodeRequestToEvent(r)},
Expand Down
4 changes: 2 additions & 2 deletions middleware_basic_auth_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// BasicAuthKeyIsValid uses a username instead of
type BasicAuthKeyIsValid struct {
TykMiddleware
*TykMiddleware
}

// New lets you do any initialisations for the object can be done here
Expand All @@ -24,7 +24,7 @@ func (k *BasicAuthKeyIsValid) GetConfig() (interface{}, error) {
}

// requestForBasicAuth sends error code and message along with WWW-Authenticate header to client.
func (k *BasicAuthKeyIsValid) requestForBasicAuth(w http.ResponseWriter,msg string)(error,int){
func (k *BasicAuthKeyIsValid) requestForBasicAuth(w http.ResponseWriter, msg string) (error, int) {
authReply := "Basic realm=\"" + k.TykMiddleware.Spec.Name + "\""

w.Header().Add("WWW-Authenticate", authReply)
Expand Down
6 changes: 3 additions & 3 deletions middleware_basic_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func getBasicAuthChain(spec APISpec) http.Handler {
spec.Init(&redisStore, &redisStore, healthStore, orgStore)
remote, _ := url.Parse("http://lonelycode.com/")
proxy := TykNewSingleHostReverseProxy(remote, &spec)
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, spec))
tykMiddleware := TykMiddleware{spec, proxy}
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, &spec))
tykMiddleware := &TykMiddleware{&spec, proxy}
chain := alice.New(
CreateMiddleware(&IPWhiteListMiddleware{tykMiddleware}, tykMiddleware),
CreateMiddleware(&BasicAuthKeyIsValid{tykMiddleware}, tykMiddleware),
CreateMiddleware(&VersionCheck{TykMiddleware:tykMiddleware}, tykMiddleware),
CreateMiddleware(&VersionCheck{TykMiddleware: tykMiddleware}, tykMiddleware),
CreateMiddleware(&KeyExpired{tykMiddleware}, tykMiddleware),
CreateMiddleware(&AccessRightsCheck{tykMiddleware}, tykMiddleware),
CreateMiddleware(&RateLimitAndQuotaCheck{tykMiddleware}, tykMiddleware)).Then(proxyHandler)
Expand Down
2 changes: 1 addition & 1 deletion middleware_check_HMAC_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const HMACClockSkewLimitInMs float64 = 1000

// HMACMiddleware will check if the request has a signature, and if the request is allowed through
type HMACMiddleware struct {
TykMiddleware
*TykMiddleware
}

func (hm *HMACMiddleware) authorizationError(w http.ResponseWriter, r *http.Request) (error, int) {
Expand Down
6 changes: 3 additions & 3 deletions middleware_check_HMAC_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func getHMACAuthChain(spec APISpec) http.Handler {
spec.Init(&redisStore, &redisStore, healthStore, orgStore)
remote, _ := url.Parse("http://lonelycode.com/")
proxy := TykNewSingleHostReverseProxy(remote, &spec)
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, spec))
tykMiddleware := TykMiddleware{spec, proxy}
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, &spec))
tykMiddleware := &TykMiddleware{&spec, proxy}
chain := alice.New(
CreateMiddleware(&IPWhiteListMiddleware{tykMiddleware}, tykMiddleware),
CreateMiddleware(&HMACMiddleware{tykMiddleware}, tykMiddleware),
CreateMiddleware(&VersionCheck{TykMiddleware:tykMiddleware}, tykMiddleware),
CreateMiddleware(&VersionCheck{TykMiddleware: tykMiddleware}, tykMiddleware),
CreateMiddleware(&KeyExpired{tykMiddleware}, tykMiddleware),
CreateMiddleware(&AccessRightsCheck{tykMiddleware}, tykMiddleware),
CreateMiddleware(&RateLimitAndQuotaCheck{tykMiddleware}, tykMiddleware)).Then(proxyHandler)
Expand Down
2 changes: 1 addition & 1 deletion middleware_granular_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// GranularAccessMiddleware will check if a URL is specifically enabled for the key
type GranularAccessMiddleware struct {
TykMiddleware
*TykMiddleware
}

type GranularAccessMiddlewareConfig struct{}
Expand Down
2 changes: 1 addition & 1 deletion middleware_ip_whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// IPWhiteListMiddleware lets you define a list of IPs to allow upstream
type IPWhiteListMiddleware struct {
TykMiddleware
*TykMiddleware
}

// New lets you do any initialisations for the object can be done here
Expand Down
2 changes: 1 addition & 1 deletion middleware_key_expired_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// KeyExpired middleware will check if the requesting key is expired or not. It makes use of the authManager to do so.
type KeyExpired struct {
TykMiddleware
*TykMiddleware
}

// New lets you do any initialisations for the object can be done here
Expand Down
2 changes: 1 addition & 1 deletion middleware_modify_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// TransformMiddleware is a middleware that will apply a template to a request body to transform it's contents ready for an upstream API
type TransformHeaders struct {
TykMiddleware
*TykMiddleware
}

type TransformHeadersConfig struct{}
Expand Down
2 changes: 1 addition & 1 deletion middleware_oauth2_key_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Oauth2KeyExists will check if the key being used to access the API is in the request data,
// and then if the key is in the storage engine
type Oauth2KeyExists struct {
TykMiddleware
*TykMiddleware
}

// New lets you do any initialisations for the object can be done here
Expand Down
2 changes: 1 addition & 1 deletion middleware_organisation_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// RateLimitAndQuotaCheck will check the incomming request and key whether it is within it's quota and
// within it's rate limit, it makes use of the SessionLimiter object to do this
type OrganizationMonitor struct {
TykMiddleware
*TykMiddleware
sessionlimiter SessionLimiter
mon Monitor
}
Expand Down
4 changes: 1 addition & 3 deletions middleware_rate_limiting.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// RateLimitAndQuotaCheck will check the incomming request and key whether it is within it's quota and
// within it's rate limit, it makes use of the SessionLimiter object to do this
type RateLimitAndQuotaCheck struct {
TykMiddleware
*TykMiddleware
}

// New lets you do any initialisations for the object can be done here
Expand All @@ -28,8 +28,6 @@ func (k *RateLimitAndQuotaCheck) ProcessRequest(w http.ResponseWriter, r *http.R
thisSessionState := context.Get(r, SessionData).(SessionState)
authHeaderValue := context.Get(r, AuthHeaderValue).(string)



storeRef := k.Spec.SessionManager.GetStore()
forwardMessage, reason := sessionLimiter.ForwardMessage(&thisSessionState, authHeaderValue, storeRef)

Expand Down
2 changes: 1 addition & 1 deletion middleware_redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (

// RedisCacheMiddleware is a caching middleware that will pull data from Redis instead of the upstream proxy
type RedisCacheMiddleware struct {
TykMiddleware
*TykMiddleware
CacheStore StorageHandler
sh SuccessHandler
}
Expand Down
2 changes: 1 addition & 1 deletion middleware_sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// ModifiedMiddleware is a sample custom middleware component, must inherit TykMiddleware
// so you have access to spec and definition data
type ModifiedMiddleware struct {
TykMiddleware
*TykMiddleware
}

type ModifiedMiddlewareConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion middleware_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// TransformMiddleware is a middleware that will apply a template to a request body to transform it's contents ready for an upstream API
type TransformMiddleware struct {
TykMiddleware
*TykMiddleware
}

type TransformMiddlewareConfig struct{}
Expand Down
2 changes: 1 addition & 1 deletion middleware_version_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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
*TykMiddleware
sh SuccessHandler
}

Expand Down
6 changes: 3 additions & 3 deletions oauth_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ func getOAuthChain(spec APISpec, Muxer *http.ServeMux) {
addOAuthHandlers(&spec, Muxer, true)
remote, _ := url.Parse("http://lonelycode.com/")
proxy := TykNewSingleHostReverseProxy(remote, &spec)
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, spec))
tykMiddleware := TykMiddleware{spec, proxy}
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, &spec))
tykMiddleware := &TykMiddleware{&spec, proxy}
chain := alice.New(
CreateMiddleware(&VersionCheck{TykMiddleware:tykMiddleware}, tykMiddleware),
CreateMiddleware(&VersionCheck{TykMiddleware: tykMiddleware}, tykMiddleware),
CreateMiddleware(&Oauth2KeyExists{tykMiddleware}, tykMiddleware),
CreateMiddleware(&KeyExpired{tykMiddleware}, tykMiddleware),
CreateMiddleware(&AccessRightsCheck{tykMiddleware}, tykMiddleware),
Expand Down
2 changes: 1 addition & 1 deletion plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (nopCloser) Close() error {

// DynamicMiddleware is a generic middleware that will execute JS code before continuing
type DynamicMiddleware struct {
TykMiddleware
*TykMiddleware
MiddlewareClassName string
Pre bool
UseSession bool
Expand Down

0 comments on commit 34cce45

Please sign in to comment.