backnero commented on issue #2855:
URL: https://github.com/apache/apisix/issues/2855#issuecomment-734712258
```
local core = require("apisix.core")
local ipairs = ipairs
local ngx = ngx
local ngx_time = ngx.time
local sub_str = string.sub
local plugin_name = "dev"
local http = require("resty.http")
local json = require("apisix.core.json")
local type = type
local string = string
-- local router = require("apisix.router")
-- local get_routes = router.http_routes
-- local profile = require("apisix.core.profile")
-- local lfs = require("lfs")
-- local re_find = ngx.re.find
local yaml = require("tinyyaml")
-- local apisix_yaml_path = profile:yaml_path("apisix")
local loadstring = loadstring
local user_routes
local router = require("resty.radixtree")
local schema = {
type = "object",
properties = {
},
}
local _M = {
version = 0.1,
priority = 70000,
type = 'auth',
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
core.log.info("input conf: ", core.json.delay_encode(conf))
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
return true
end
local function fail_response(message, init_values)
local response = init_values or {}
response.message = message
return response
end
local function success_response(message, init_values)
local response = init_values or {}
response.message = message
return response
end
local function create_radixtree_router(routes)
local uri_routes = {}
local uri_router
routes = routes or {}
core.table.clear(uri_routes)
for _, route in ipairs(routes) do
core.log.error("route for: ", core.json.delay_encode(route, true))
if type(route) == "table" then
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.error("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.error("route items:2 ",core.json.uri_routes)
uri_router = router.new(uri_routes)
core.response.exit(200, success_response(nil, {routes = uri_routes}))
end
function get_routes()
local user_routes, err = core.config.new("/routes", {
automatic = true,
item_schema = core.schema.route,
filter = filter,
})
create_radixtree_router(user_routes.values)
-- core.log.error("route items:1 ", core.json.uri_routes)
end
function _M.api()
return {
{
methods = {"GET"},
uri = "/apisix/plugin/routes",
handler = get_routes,
}
}
end
return _M
```
response
```
{
"routes": {}
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]