-
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.
change: don't treat route segment with ':' as parameter by default (#…
- Loading branch information
1 parent
b80020e
commit 81c1c7c
Showing
18 changed files
with
467 additions
and
127 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
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,118 @@ | ||
-- | ||
-- 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 radixtree = require("resty.radixtree") | ||
local router = require("apisix.utils.router") | ||
local core = require("apisix.core") | ||
local http_route = require("apisix.http.route") | ||
local ipairs = ipairs | ||
local type = type | ||
local error = error | ||
local loadstring = loadstring | ||
|
||
|
||
local _M = {} | ||
|
||
|
||
function _M.create_radixtree_uri_router(routes, uri_routes, with_parameter) | ||
routes = routes or {} | ||
|
||
core.table.clear(uri_routes) | ||
|
||
for _, route in ipairs(routes) do | ||
if type(route) == "table" then | ||
local status = core.table.try_read_attr(route, "value", "status") | ||
-- check the status | ||
if status and status == 0 then | ||
goto CONTINUE | ||
end | ||
|
||
local filter_fun, err | ||
if route.value.filter_func then | ||
filter_fun, err = loadstring( | ||
"return " .. route.value.filter_func, | ||
"router#" .. route.value.id) | ||
if not filter_fun then | ||
core.log.error("failed to load filter function: ", err, | ||
" route id: ", route.value.id) | ||
goto CONTINUE | ||
end | ||
|
||
filter_fun = filter_fun() | ||
end | ||
|
||
core.log.info("insert uri route: ", | ||
core.json.delay_encode(route.value)) | ||
core.table.insert(uri_routes, { | ||
paths = route.value.uris or route.value.uri, | ||
methods = route.value.methods, | ||
priority = route.value.priority, | ||
hosts = route.value.hosts or route.value.host, | ||
remote_addrs = route.value.remote_addrs | ||
or route.value.remote_addr, | ||
vars = route.value.vars, | ||
filter_fun = filter_fun, | ||
handler = function (api_ctx, match_opts) | ||
api_ctx.matched_params = nil | ||
api_ctx.matched_route = route | ||
api_ctx.curr_req_matched = match_opts.matched | ||
end | ||
}) | ||
|
||
::CONTINUE:: | ||
end | ||
end | ||
|
||
core.log.info("route items: ", core.json.delay_encode(uri_routes, true)) | ||
|
||
if with_parameter then | ||
return radixtree.new(uri_routes) | ||
else | ||
return router.new(uri_routes) | ||
end | ||
end | ||
|
||
|
||
function _M.match_uri(uri_router, match_opts, api_ctx) | ||
core.table.clear(match_opts) | ||
match_opts.method = api_ctx.var.request_method | ||
match_opts.host = api_ctx.var.host | ||
match_opts.remote_addr = api_ctx.var.remote_addr | ||
match_opts.vars = api_ctx.var | ||
match_opts.matched = core.tablepool.fetch("matched_route_record", 0, 4) | ||
|
||
local ok = uri_router:dispatch(api_ctx.var.uri, match_opts, api_ctx, match_opts) | ||
return ok | ||
end | ||
|
||
|
||
function _M.init_worker(filter) | ||
local user_routes, err = core.config.new("/routes", { | ||
automatic = true, | ||
item_schema = core.schema.route, | ||
checker = http_route.check_route, | ||
filter = filter, | ||
}) | ||
if not user_routes then | ||
error("failed to create etcd instance for fetching /routes : " .. err) | ||
end | ||
|
||
return user_routes | ||
end | ||
|
||
|
||
return _M |
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,46 @@ | ||
-- | ||
-- 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 core = require("apisix.core") | ||
local base_router = require("apisix.http.router.base") | ||
local cached_version | ||
|
||
|
||
local _M = {} | ||
|
||
|
||
local uri_routes = {} | ||
local uri_router | ||
local match_opts = {} | ||
function _M.match(api_ctx) | ||
local user_routes = _M.user_routes | ||
if not cached_version or cached_version ~= user_routes.conf_version then | ||
uri_router = base_router.create_radixtree_uri_router(user_routes.values, | ||
uri_routes, true) | ||
cached_version = user_routes.conf_version | ||
end | ||
|
||
if not uri_router then | ||
core.log.error("failed to fetch valid `uri_with_parameter` router: ") | ||
return true | ||
end | ||
|
||
return base_router.match_uri(uri_router, match_opts, api_ctx) | ||
end | ||
|
||
|
||
return _M |
Oops, something went wrong.