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 03:42:09 UTC

[GitHub] [apisix] spacewander opened a new pull request #3048: feat: add control API

spacewander opened a new pull request #3048:
URL: https://github.com/apache/apisix/pull/3048


   Signed-off-by: spacewander <sp...@gmail.com>
   
   ### What this PR does / why we need it:
   <!--- Why is this change required? What problem does it solve? -->
   <!--- If it fixes an open issue, please link to the issue here. -->
   
   ### Pre-submission checklist:
   
   * [ ] Did you explain what problem does this PR solve? Or what new features have been added?
   * [x] Have you added corresponding test cases?
   * [x] Have you modified the corresponding document?
   * [ ] Is this PR backward compatible? **If it is not backward compatible, please discuss on the [mailing list](https://github.com/apache/apisix/tree/master#community) first**
   


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



[GitHub] [apisix] spacewander merged pull request #3048: feat: add control API

Posted by GitBox <gi...@apache.org>.
spacewander merged pull request #3048:
URL: https://github.com/apache/apisix/pull/3048


   


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



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

Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on pull request #3048: feat: add control API

Posted by GitBox <gi...@apache.org>.
spacewander commented on pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#issuecomment-745744682


   Related issue: #2798 #3022 


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



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

Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r543878209



##########
File path: .travis/apisix_cli_test/test_control.sh
##########
@@ -0,0 +1,118 @@
+#!/usr/bin/env bash
+
+#
+# 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.
+#
+

Review comment:
       `set -ex`




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



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

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r543808879



##########
File path: doc/control-api.md
##########
@@ -0,0 +1,51 @@
+<!--
+#
+# 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.
+#
+-->
+
+The control API can be used to
+* expose APISIX internal state
+* control the behavior of a single isolate APISIX data panel
+
+By default, the control API server is enabled and listens to `127.0.0.1:9090`. You can change it via
+the `control` section under `apisix` in `conf/config.yaml`:
+
+```yaml
+apisix:
+  ...
+  enable_control: true
+  control:
+    ip: "127.0.0.1"
+    port: 9090
+```
+
+Note that the control API server should not be configured to listen the public traffic!

Review comment:
       Updated




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



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

Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r545123231



##########
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:
       thx, got it




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



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

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r543929390



##########
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:
       updated




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



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

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r543261850



##########
File path: doc/control-api.md
##########
@@ -0,0 +1,51 @@
+<!--
+#
+# 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.
+#
+-->
+
+The control API can be used to
+* expose APISIX internal state
+* control the behavior of a single isolate APISIX data panel
+
+By default, the control API server is enabled and listens to `127.0.0.1:9090`. You can change it via
+the `control` section under `apisix` in `conf/config.yaml`:
+
+```yaml
+apisix:
+  ...
+  enable_control: true
+  control:
+    ip: "127.0.0.1"
+    port: 9090
+```
+
+Note that the control API server should not be configured to listen the public traffic!
+
+## Control API Added via plugin
+
+Plugin can add its control API when it is enabled.
+If a plugin adds such control API, please refer each plugin's documentation for those APIs.

Review comment:
       @jbampton 
   Thanks!




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



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

Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r545514904



##########
File path: doc/control-api.md
##########
@@ -0,0 +1,51 @@
+<!--
+#
+# 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.
+#
+-->
+
+The control API can be used to
+* expose APISIX internal state
+* control the behavior of a single isolate APISIX data panel
+
+By default, the control API server is enabled and listens to `127.0.0.1:9090`. You can change it via
+the `control` section under `apisix` in `conf/config.yaml`:
+
+```yaml
+apisix:
+  ...
+  enable_control: true
+  control:
+    ip: "127.0.0.1"
+    port: 9090
+```
+
+Note that the control API server should not be configured to listen to the public traffic!
+
+## Control API Added via plugin
+
+Plugin can add its control API when it is enabled.

Review comment:
       OK.




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



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

Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r545122934



##########
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:
       keep current way




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



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

Posted by GitBox <gi...@apache.org>.
jbampton commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r543257943



##########
File path: .travis/apisix_cli_test/test_ci_only.sh
##########
@@ -17,18 +17,10 @@
 # limitations under the License.
 #
 
-# This file is like apisix_cli_test.sh, but requires extra dependencies which
+# This file is like other test_*.sh, but requires extra dependencies which
 # you don't need them in daily development.

Review comment:
       ```suggestion
   # you don't need in daily development.
   ```

##########
File path: doc/control-api.md
##########
@@ -0,0 +1,51 @@
+<!--
+#
+# 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.
+#
+-->
+
+The control API can be used to
+* expose APISIX internal state
+* control the behavior of a single isolate APISIX data panel
+
+By default, the control API server is enabled and listens to `127.0.0.1:9090`. You can change it via
+the `control` section under `apisix` in `conf/config.yaml`:
+
+```yaml
+apisix:
+  ...
+  enable_control: true
+  control:
+    ip: "127.0.0.1"
+    port: 9090
+```
+
+Note that the control API server should not be configured to listen the public traffic!
+
+## Control API Added via plugin
+
+Plugin can add its control API when it is enabled.
+If a plugin adds such control API, please refer each plugin's documentation for those APIs.

Review comment:
       ```suggestion
   If a plugin adds such control API, please refer to each plugin's documentation for those APIs.
   ```




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



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

Posted by GitBox <gi...@apache.org>.
jbampton commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r543267735



##########
File path: doc/control-api.md
##########
@@ -0,0 +1,51 @@
+<!--
+#
+# 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.
+#
+-->
+
+The control API can be used to
+* expose APISIX internal state
+* control the behavior of a single isolate APISIX data panel
+
+By default, the control API server is enabled and listens to `127.0.0.1:9090`. You can change it via
+the `control` section under `apisix` in `conf/config.yaml`:
+
+```yaml
+apisix:
+  ...
+  enable_control: true
+  control:
+    ip: "127.0.0.1"
+    port: 9090
+```
+
+Note that the control API server should not be configured to listen the public traffic!
+
+## Control API Added via plugin
+
+Plugin can add its control API when it is enabled.
+If a plugin adds such control API, please refer to each plugin's documentation for those APIs.

Review comment:
       ```suggestion
   If a plugin adds such a control API, please refer to each plugin's documentation for those APIs.
   ```

##########
File path: doc/control-api.md
##########
@@ -0,0 +1,51 @@
+<!--
+#
+# 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.
+#
+-->
+
+The control API can be used to
+* expose APISIX internal state
+* control the behavior of a single isolate APISIX data panel
+
+By default, the control API server is enabled and listens to `127.0.0.1:9090`. You can change it via
+the `control` section under `apisix` in `conf/config.yaml`:
+
+```yaml
+apisix:
+  ...
+  enable_control: true
+  control:
+    ip: "127.0.0.1"
+    port: 9090
+```
+
+Note that the control API server should not be configured to listen the public traffic!

Review comment:
       ```suggestion
   Note that the control API server should not be configured to listen to public traffic!
   ```




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



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

Posted by GitBox <gi...@apache.org>.
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



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

Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r545513373



##########
File path: doc/control-api.md
##########
@@ -0,0 +1,51 @@
+<!--
+#
+# 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.
+#
+-->
+
+The control API can be used to
+* expose APISIX internal state
+* control the behavior of a single isolate APISIX data panel
+
+By default, the control API server is enabled and listens to `127.0.0.1:9090`. You can change it via
+the `control` section under `apisix` in `conf/config.yaml`:
+
+```yaml
+apisix:
+  ...
+  enable_control: true
+  control:
+    ip: "127.0.0.1"
+    port: 9090
+```
+
+Note that the control API server should not be configured to listen to the public traffic!
+
+## Control API Added via plugin
+
+Plugin can add its control API when it is enabled.

Review comment:
       Shall we add the control API example in plugin-develop.md?

##########
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:
       Agree, and we need more comments here.




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



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

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r543799370



##########
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 already wrote it https://github.com/apache/apisix/issues/2798 a month ago.
   And https://github.com/apache/apisix/issues/3022 shows why we need this feature.




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



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

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #3048:
URL: https://github.com/apache/apisix/pull/3048#discussion_r545514382



##########
File path: doc/control-api.md
##########
@@ -0,0 +1,51 @@
+<!--
+#
+# 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.
+#
+-->
+
+The control API can be used to
+* expose APISIX internal state
+* control the behavior of a single isolate APISIX data panel
+
+By default, the control API server is enabled and listens to `127.0.0.1:9090`. You can change it via
+the `control` section under `apisix` in `conf/config.yaml`:
+
+```yaml
+apisix:
+  ...
+  enable_control: true
+  control:
+    ip: "127.0.0.1"
+    port: 9090
+```
+
+Note that the control API server should not be configured to listen to the public traffic!
+
+## Control API Added via plugin
+
+Plugin can add its control API when it is enabled.

Review comment:
       Yes, but we can do it later. It seems the `plugin-develop.md` is outdated and need to be rewritten.




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