github.com/dbernstein1/tyk@v2.9.0-beta9-dl-apic+incompatible/gateway/mw_track_endpoints.go (about)

     1  package gateway
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/TykTechnologies/tyk/apidef"
     7  )
     8  
     9  // TrackEndpointMiddleware sets context variables to enable or disable whether Tyk should record analytitcs for a specific path.
    10  type TrackEndpointMiddleware struct {
    11  	BaseMiddleware
    12  }
    13  
    14  func (t *TrackEndpointMiddleware) Name() string {
    15  	return "TrackEndpointMiddleware"
    16  }
    17  
    18  func (t *TrackEndpointMiddleware) EnabledForSpec() bool {
    19  	if !t.Spec.GlobalConfig.EnableAnalytics || t.Spec.DoNotTrack {
    20  		return false
    21  	}
    22  
    23  	for _, version := range t.Spec.VersionData.Versions {
    24  		if len(version.ExtendedPaths.TrackEndpoints) > 0 {
    25  			return true
    26  		}
    27  	}
    28  
    29  	return false
    30  }
    31  
    32  // ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
    33  func (t *TrackEndpointMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
    34  	_, versionPaths, _, _ := t.Spec.Version(r)
    35  	foundTracked, metaTrack := t.Spec.CheckSpecMatchesStatus(r, versionPaths, RequestTracked)
    36  	if foundTracked {
    37  		ctxSetTrackedPath(r, metaTrack.(*apidef.TrackEndpointMeta).Path)
    38  	}
    39  
    40  	foundDnTrack, _ := t.Spec.CheckSpecMatchesStatus(r, versionPaths, RequestNotTracked)
    41  	if foundDnTrack {
    42  		ctxSetDoNotTrack(r, true)
    43  	}
    44  
    45  	return nil, http.StatusOK
    46  }