spacewander commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r543801690



##########
File path: apisix/control/router.lua
##########
@@ -0,0 +1,107 @@
+--
+-- 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 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,
+                paths = route.uri,
+                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 = {}
+
+function fetch_control_api_router()
+    core.table.clear(routes)
+
+    register_api_routes(routes, 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(routes, api_routes)
+        end
+    end
+
+    return router.new(routes)
+end
+
+end -- do
+
+
+do
+    local match_opts = {}
+    local cached_version
+    local router
+
+function _M.match(uri)
+    if not core.string.has_prefix(uri, "/v1/") then

Review comment:
       This change aims to limit the user defined urls under the `/v1/` prefix. 
So that we can maintain API versioning without hardcoding v1. But it is OK to 
reimplement this via radixtree.




----------------------------------------------------------------
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]


Reply via email to