You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/12/15 13:51:34 UTC

[GitHub] [apisix] membphis commented on a change in pull request #3048: feat: add control API

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



##########
File path: apisix/control/v1.lua
##########
@@ -0,0 +1,33 @@
+--
+-- 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 _M = {}
+
+
+function _M.schema()
+    -- stub for test yet, fill it in the next PR
+    return 200, {}
+end
+
+
+return {
+    -- /v1/schema
+    {
+        methods = {"GET"},
+        uri = "/schema",

Review comment:
       use `uris` is better, we'll remove `uri` later

##########
File path: apisix/plugins/example-plugin.lua
##########
@@ -109,4 +110,28 @@ function _M.access(conf, ctx)
 end
 
 
+local function hello()
+    local args = ngx.req.get_uri_args()
+    if args["json"] then
+        return 200, {msg = "world"}
+    else
+        return 200, "world\n"
+    end
+end
+
+
+function _M.control_api(ver)

Review comment:
       I did not understand why we need this feature. 
   
   I think you need to write a proposal first.

##########
File path: conf/config-default.yaml
##########
@@ -114,6 +114,10 @@ apisix:
     key_encrypt_salt: "edd1c9f0985e76a2"    #  If not set, will save origin ssl key into etcd.
                                             #  If set this, must be a string of length 16. And it will encrypt ssl key with AES-128-CBC
                                             #  !!! So do not change it after saving your ssl, it can't decrypt the ssl keys have be saved if you change !!
+  enable_control: true

Review comment:
       I think the default value should be`false`

##########
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:
       I don't like this kind of "hack".
   
   We should use `resty-radixtree` router, it is powerful enough




----------------------------------------------------------------
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:
users@infra.apache.org