-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: spacewander <[email protected]> Co-authored-by: John Bampton <[email protected]
- Loading branch information
1 parent
952854d
commit 924a30d
Showing
16 changed files
with
497 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
# 'make init' operates scripts and related configuration files in the current directory | ||
# The 'apisix' command is a command in the /usr/local/apisix, | ||
# and the configuration file for the operation is in the /usr/local/apisix/conf | ||
|
||
set -ex | ||
|
||
clean_up() { | ||
make stop || true | ||
git checkout conf/config.yaml | ||
} | ||
|
||
trap clean_up EXIT | ||
|
||
unset APISIX_PROFILE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#!/usr/bin/env bash | ||
|
||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
. ./.travis/apisix_cli_test/common.sh | ||
|
||
# control server | ||
echo ' | ||
apisix: | ||
enable_control: true | ||
' > conf/config.yaml | ||
|
||
make init | ||
|
||
if ! grep "listen 127.0.0.1:9090;" conf/nginx.conf > /dev/null; then | ||
echo "failed: find default address for control server" | ||
exit 1 | ||
fi | ||
|
||
make run | ||
|
||
sleep 0.1 | ||
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9090/v1/schema) | ||
|
||
if [ ! $code -eq 200 ]; then | ||
echo "failed: access control server" | ||
exit 1 | ||
fi | ||
|
||
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9090/v0/schema) | ||
|
||
if [ ! $code -eq 404 ]; then | ||
echo "failed: handle route not found" | ||
exit 1 | ||
fi | ||
|
||
make stop | ||
|
||
echo ' | ||
apisix: | ||
enable_control: true | ||
control: | ||
ip: 127.0.0.2 | ||
' > conf/config.yaml | ||
|
||
make init | ||
|
||
if ! grep "listen 127.0.0.2:9090;" conf/nginx.conf > /dev/null; then | ||
echo "failed: customize address for control server" | ||
exit 1 | ||
fi | ||
|
||
make run | ||
|
||
sleep 0.1 | ||
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.2:9090/v1/schema) | ||
|
||
if [ ! $code -eq 200 ]; then | ||
echo "failed: access control server" | ||
exit 1 | ||
fi | ||
|
||
make stop | ||
|
||
echo ' | ||
apisix: | ||
enable_control: true | ||
control: | ||
port: 9091 | ||
' > conf/config.yaml | ||
|
||
make init | ||
|
||
if ! grep "listen 127.0.0.1:9091;" conf/nginx.conf > /dev/null; then | ||
echo "failed: customize address for control server" | ||
exit 1 | ||
fi | ||
|
||
make run | ||
|
||
sleep 0.1 | ||
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9091/v1/schema) | ||
|
||
if [ ! $code -eq 200 ]; then | ||
echo "failed: access control server" | ||
exit 1 | ||
fi | ||
|
||
make stop | ||
|
||
echo ' | ||
apisix: | ||
enable_control: false | ||
' > conf/config.yaml | ||
|
||
make init | ||
|
||
if grep "listen 127.0.0.1:9090;" conf/nginx.conf > /dev/null; then | ||
echo "failed: disable control server" | ||
exit 1 | ||
fi | ||
|
||
echo "pass: access control server" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
-- | ||
-- Licensed to the Apache Software Foundation (ASF) under one or more | ||
-- contributor license agreements. See the NOTICE file distributed with | ||
-- this work for additional information regarding copyright ownership. | ||
-- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
-- (the "License"); you may not use this file except in compliance with | ||
-- the License. You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
local require = require | ||
local router = require("resty.radixtree") | ||
local builtin_v1_routes = require("apisix.control.v1") | ||
local plugin_mod = require("apisix.plugin") | ||
local core = require("apisix.core") | ||
local str_sub = string.sub | ||
local ipairs = ipairs | ||
local type = type | ||
local ngx = ngx | ||
local get_method = ngx.req.get_method | ||
|
||
|
||
local _M = {} | ||
local current_version = 1 | ||
|
||
|
||
local fetch_control_api_router | ||
do | ||
local function register_api_routes(routes, api_routes) | ||
for _, route in ipairs(api_routes) do | ||
core.table.insert(routes, { | ||
methods = route.methods, | ||
-- note that it is 'uris' for control API, which is an array of strings | ||
paths = route.uris, | ||
handler = function (api_ctx) | ||
local code, body = route.handler(api_ctx) | ||
if code or body then | ||
if type(body) == "table" and ngx.header["Content-Type"] == nil then | ||
core.response.set_header("Content-Type", "application/json") | ||
end | ||
|
||
core.response.exit(code, body) | ||
end | ||
end | ||
}) | ||
end | ||
end | ||
|
||
local routes = {} | ||
local v1_routes = {} | ||
local function empty_func() end | ||
|
||
function fetch_control_api_router() | ||
core.table.clear(v1_routes) | ||
|
||
register_api_routes(v1_routes, builtin_v1_routes) | ||
|
||
for _, plugin in ipairs(plugin_mod.plugins) do | ||
local api_fun = plugin.control_api | ||
if api_fun then | ||
local api_routes = api_fun(current_version) | ||
register_api_routes(v1_routes, api_routes) | ||
end | ||
end | ||
|
||
local v1_router, err = router.new(v1_routes) | ||
if not v1_router then | ||
return nil, err | ||
end | ||
|
||
core.table.clear(routes) | ||
core.table.insert(routes, { | ||
paths = {"/v1/*"}, | ||
filter_fun = function(vars, opts, ...) | ||
local uri = str_sub(vars.uri, #"/v1" + 1) | ||
return v1_router:dispatch(uri, opts, ...) | ||
end, | ||
handler = empty_func, | ||
}) | ||
|
||
return router.new(routes) | ||
end | ||
|
||
end -- do | ||
|
||
|
||
do | ||
local match_opts = {} | ||
local cached_version | ||
local router | ||
|
||
function _M.match(uri) | ||
if cached_version ~= plugin_mod.load_times then | ||
local err | ||
router, err = fetch_control_api_router() | ||
if router == nil then | ||
core.log.error("failed to fetch valid api router: ", err) | ||
return false | ||
end | ||
|
||
cached_version = plugin_mod.load_times | ||
end | ||
|
||
core.table.clear(match_opts) | ||
match_opts.method = get_method() | ||
|
||
return router:dispatch(uri, match_opts) | ||
end | ||
|
||
end -- do | ||
|
||
|
||
return _M |
Oops, something went wrong.