Skip to content

Commit

Permalink
Gateway should skip inactive APIs. Resolves #1151
Browse files Browse the repository at this point in the history
  • Loading branch information
asoorm committed Sep 29, 2017
1 parent 6ee678c commit 3910825
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 17 deletions.
11 changes: 7 additions & 4 deletions api_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const sampleDefiniton = `{
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `"
}
},
"active": true
}`

const nonExpiringDef = `{
Expand All @@ -64,7 +65,8 @@ const nonExpiringDef = `{
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `"
}
},
"active": true
}`

const nonExpiringMultiDef = `{
Expand Down Expand Up @@ -98,7 +100,8 @@ const nonExpiringMultiDef = `{
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `"
}
},
"active": true
}`

func createDefinitionFromString(defStr string) *APISpec {
Expand Down Expand Up @@ -378,7 +381,7 @@ func TestGetAPISpecsDashboardSuccess(t *testing.T) {
// Mock Dashboard
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/system/apis" {
w.Write([]byte(`{"Status": "OK", "Nonce": "1", "Message": [{"api_definition": {}}]}`))
w.Write([]byte(`{"Status": "OK", "Nonce": "1", "Message": [{"api_definition": {"active": true}}]}`))
} else {
t.Fatal("Unknown dashboard API request", r)
}
Expand Down
15 changes: 13 additions & 2 deletions api_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ func prepareStorage() (*RedisClusterStorageManager, *RedisClusterStorageManager,
return &redisStore, &redisOrgStore, healthStore, &rpcAuthStore, &rpcOrgStore
}

func skipSpecBecauseInvalid(spec *APISpec) bool {
func skipSpec(spec *APISpec) bool {

// Remove inactive APIs from the specs
if !spec.Active {
log.WithFields(logrus.Fields{
"prefix": "main",
"api_name": spec.Name,
"domain": spec.Domain,
}).Info("Skipping Inactive.")
return true
}

if spec.Proxy.ListenPath == "" {
log.WithFields(logrus.Fields{
Expand Down Expand Up @@ -109,7 +119,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int,
"api_name": spec.Name,
}).Info("Loading API")

if skipSpecBecauseInvalid(spec) {
if skipSpec(spec) {
log.WithFields(logrus.Fields{
"prefix": "main",
"api_name": spec.Name,
Expand Down Expand Up @@ -555,6 +565,7 @@ func loadApps(specs []*APISpec, muxer *mux.Router) {
loadList := make([]*ChainObject, len(specs))
apisByListen := countApisByListenHash(specs)
for i, spec := range specs {

go func(spec *APISpec, i int) {
subrouter := muxer
// Handle custom domains
Expand Down
3 changes: 2 additions & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const apiTestDef = `{
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `"
}
},
"active": true
}`

func loadSampleAPI(t *testing.T, def string) {
Expand Down
3 changes: 2 additions & 1 deletion apps/app_sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"target_url": "http://httpbin.org",
"strip_listen_path": true
},
"enable_batch_request_support": true
"enable_batch_request_support": true,
"active": true
}
3 changes: 2 additions & 1 deletion apps/coprocess_app_sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@
],
"driver": "python"
},
"enable_batch_request_support": true
"enable_batch_request_support": true,
"active": true
}
3 changes: 2 additions & 1 deletion apps/coprocess_app_sample_protected.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
},
"driver": "python"
},
"enable_batch_request_support": true
"enable_batch_request_support": true,
"active": true
}
3 changes: 2 additions & 1 deletion apps/coprocess_grpc_app_sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
]
}
},
"enable_batch_request_support": true
"enable_batch_request_support": true,
"active": true
}
3 changes: 2 additions & 1 deletion apps/coprocess_grpc_app_sample_protected.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@
"session_lifetime": 35
},
"custom_middleware_bundle": "test-bundle",
"enable_batch_request_support": true
"enable_batch_request_support": true,
"active": true
}
3 changes: 2 additions & 1 deletion apps/coprocess_lua_app_sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
],
"driver": "lua"
},
"enable_batch_request_support": true
"enable_batch_request_support": true,
"active": true
}
3 changes: 2 additions & 1 deletion apps/quickstart.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"target_url": "http://httpbin.org/",
"strip_listen_path": true
},
"do_not_track": true
"do_not_track": true,
"active": true
}
6 changes: 4 additions & 2 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,8 @@ const sampleAPI = `{
"proxy": {
"listen_path": "/sample",
"target_url": "` + testHttpAny + `"
}
},
"active": true
}`

func TestListener(t *testing.T) {
Expand Down Expand Up @@ -982,7 +983,8 @@ const apiWithTykListenPathPrefix = `{
"proxy": {
"listen_path": "/tyk-foo/",
"target_url": "` + testHttpAny + `"
}
},
"active": true
}`

func TestListenPathTykPrefix(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion oauth_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const oauthDefinition = `{
"proxy": {
"listen_path": "/APIID/",
"target_url": "` + testHttpAny + `"
}
},
"active": true
}`

func getOAuthChain(spec *APISpec, muxer *mux.Router) {
Expand Down

0 comments on commit 3910825

Please sign in to comment.