Skip to content

Commit

Permalink
allow specifying default version in config
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblakeley committed Jan 8, 2018
1 parent 4a89639 commit 092af4a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 3 deletions.
7 changes: 7 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1479,3 +1479,10 @@ func ctxGetUrlRewritePath(r *http.Request) string {
}
return ""
}
func ctxGetDefaultVersion(r *http.Request) bool {
return r.Context().Value(VersionDefault) != nil
}

func ctxSetDefaultVersion(r *http.Request) {
setCtxValue(r, VersionDefault, true)
}
8 changes: 7 additions & 1 deletion api_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,8 +1032,13 @@ func (a *APISpec) Version(r *http.Request) (*apidef.VersionInfo, []URLSpec, bool
}
} else {
// Extract Version Info
// First checking for if default version is set
vname := a.getVersionFromRequest(r)
if vname == "" {
if vname == "" && a.VersionData.DefaultVersion != "" {
vname = a.VersionData.DefaultVersion
ctxSetDefaultVersion(r)
}
if vname == "" && a.VersionData.DefaultVersion == "" {
return &version, nil, false, VersionNotFound
}
// Load Version Data - General
Expand All @@ -1045,6 +1050,7 @@ func (a *APISpec) Version(r *http.Request) (*apidef.VersionInfo, []URLSpec, bool

// cache for the future
ctxSetVersionInfo(r, &version)

}

// Load path data and whitelist data for version
Expand Down
31 changes: 31 additions & 0 deletions api_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,34 @@ func (ln *customListener) Accept() (conn io.ReadWriteCloser, clientAddr string,
func (ln *customListener) Close() error {
return ln.L.Close()
}

func TestDefaultVersion(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()

buildAndLoadAPI(func(spec *APISpec) {
v1 := apidef.VersionInfo{Name: "v1"}
v1.Name = "v1"
v1.Paths.WhiteList = []string{"/foo"}

v2 := apidef.VersionInfo{Name: "v2"}
v2.Paths.WhiteList = []string{"/bar"}

spec.VersionDefinition.Location = "url-param"
spec.VersionDefinition.Key = "v"
spec.VersionData.NotVersioned = false

spec.VersionData.Versions["v1"] = v1
spec.VersionData.Versions["v2"] = v2
spec.VersionData.DefaultVersion = "v2"
spec.Proxy.ListenPath = "/"
})

ts.Run(t, []test.TestCase{
{Path: "/foo", Code: 403}, // Not whitelisted for default v2
{Path: "/bar", Code: 200}, // Whitelisted for default v2
{Path: "/foo?v=v1", Code: 200}, // Allowed for v1
{Path: "/bar?v=v1", Code: 403}, // Not allowed for v1
}...)

}
5 changes: 3 additions & 2 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,9 @@ type APIDefinition struct {
Key string `bson:"key" json:"key"`
} `bson:"definition" json:"definition"`
VersionData struct {
NotVersioned bool `bson:"not_versioned" json:"not_versioned"`
Versions map[string]VersionInfo `bson:"versions" json:"versions"`
NotVersioned bool `bson:"not_versioned" json:"not_versioned"`
DefaultVersion string `bson:"default_version" json:"default_version"`
Versions map[string]VersionInfo `bson:"versions" json:"versions"`
} `bson:"version_data" json:"version_data"`
UptimeTests struct {
CheckList []HostCheckObject `bson:"check_list" json:"check_list"`
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ type Config struct {
LogLevel string `json:"log_level"`
Security SecurityConfig `json:"security"`
EnableKeyLogging bool `json:"enable_key_logging"`
VersionHeader bool `json:"version_header"`
}

type CertData struct {
Expand Down
3 changes: 3 additions & 0 deletions handler_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
defaultTemplateName = "error"
defaultTemplateFormat = "json"
defaultContentType = "application/json"
defaultVersionHeader = "X-Tyk-Default-Version"
)

// APIError is generic error object returned if there is something wrong with the request
Expand Down Expand Up @@ -87,6 +88,8 @@ func (e *ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, errMs

t := time.Now()

addVersionHeader(w, r)

version := e.Spec.getVersionFromRequest(r)
if version == "" {
version = "Non Versioned"
Expand Down
15 changes: 15 additions & 0 deletions handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
SessionData = iota
AuthHeaderValue
VersionData
VersionDefault
OrgSessionContext
ContextData
RetainHost
Expand Down Expand Up @@ -68,6 +69,16 @@ func tagHeaders(r *http.Request, th []string, tags []string) []string {
return tags
}

func addVersionHeader(w http.ResponseWriter, r *http.Request) {
if ctxGetDefaultVersion(r) {
if vinfo := ctxGetVersionInfo(r); vinfo != nil {
if config.Global.VersionHeader {
w.Header().Set("X-Tyk-Default-Version", vinfo.Name)
}
}
}
}

func (s *SuccessHandler) RecordHit(r *http.Request, timing int64, code int, requestCopy *http.Request, responseCopy *http.Response) {

if s.Spec.DoNotTrack {
Expand Down Expand Up @@ -219,6 +230,8 @@ func (s *SuccessHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) *http
log.Debug("Upstream Path is: ", r.URL.Path)
}

addVersionHeader(w, r)

var copiedRequest *http.Request
if recordDetail(r) {
copiedRequest = copyRequest(r)
Expand Down Expand Up @@ -260,6 +273,8 @@ func (s *SuccessHandler) ServeHTTPWithCache(w http.ResponseWriter, r *http.Reque
inRes := s.Proxy.ServeHTTPForCache(w, r)
t2 := time.Now()

addVersionHeader(w, r)

var copiedResponse *http.Response
if recordDetail(r) {
copiedResponse = copyResponse(inRes)
Expand Down
3 changes: 3 additions & 0 deletions lint/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ const confSchema = `{
}
}
},
"default_version_header":{
"type": "boolean"
},
"disable_dashboard_zeroconf": {
"type": "boolean"
},
Expand Down

0 comments on commit 092af4a

Please sign in to comment.