apisix icon indicating copy to clipboard operation
apisix copied to clipboard

I want to return routing data through the interface

Open backnero opened this issue 6 years ago • 3 comments

I want to return routing data through the interface. This is the source code of my plug-in, but now the return value is empty. How can I implement it?


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": {}
}

Originally posted by @backnero in https://github.com/apache/apisix/issues/2855#issuecomment-734712258

backnero avatar Nov 27 '20 08:11 backnero

You can submitted in PR the we can discuss it.

By the way, if you can write some use case in PR, that would be great.

membphis avatar Nov 27 '20 09:11 membphis

This issue has been marked as stale due to 350 days of inactivity. It will be closed in 2 weeks if no further activity occurs. If this issue is still relevant, please simply write any comment. Even if closed, you can still revive the issue at any time or discuss it on the [email protected] list. Thank you for your contributions.

github-actions[bot] avatar Nov 12 '21 10:11 github-actions[bot]

This issue has been closed due to lack of activity. If you think that is incorrect, or the issue requires additional review, you can revive the issue at any time.

github-actions[bot] avatar Nov 26 '21 10:11 github-actions[bot]