You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "kingluo (via GitHub)" <gi...@apache.org> on 2023/05/24 03:42:04 UTC

[GitHub] [apisix] kingluo opened a new pull request, #9534: feat: add service path_prefix

kingluo opened a new pull request, #9534:
URL: https://github.com/apache/apisix/pull/9534

   ### Description
   
   <!-- Please include a summary of the change and which issue is fixed. -->
   <!-- Please also include relevant motivation and context. -->
   
   If `path_prefix` specified in service, it adds the prefix to the request uri.
   
   ### Checklist
   
   - [x] I have explained the need for this PR and the problem it solves
   - [x] I have explained the changes or the new features added to this PR
   - [x] I have added tests corresponding to this change
   - [x] I have updated the documentation to reflect this change
   - [x] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
   
   <!--
   
   Note
   
   1. Mark the PR as draft until it's ready to be reviewed.
   2. Always add/update tests for any changes unless you have a good reason.
   3. Always update the documentation to reflect the changes made in the PR.
   4. Make a new commit to resolve conversations instead of `push -f`.
   5. To resolve merge conflicts, merge master instead of rebasing.
   6. Use "request review" to notify the reviewer after making changes.
   7. Only a reviewer can mark a conversation as resolved.
   
   -->
   


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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #9534: feat: add service path_prefix

Posted by "kingluo (via GitHub)" <gi...@apache.org>.
kingluo commented on code in PR #9534:
URL: https://github.com/apache/apisix/pull/9534#discussion_r1203531031


##########
t/node/service-path-prefix.t:
##########
@@ -0,0 +1,78 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+log_level('warn');
+no_root_location();
+no_shuffle();
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: test service path-prefix
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+
+            assert(t.test('/apisix/admin/services/1',
+                ngx.HTTP_PUT,
+                [[
+                    {
+                        "plugins": {
+                            "proxy-rewrite": {
+                                "regex_uri": ["^/foo/(.*)","/$1"]
+                            }
+                        },
+                        "path_prefix": "/foo",
+                        "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                        }
+                    }
+                ]]
+            ))
+            ngx.sleep(0.5)
+
+            assert(t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[
+                    {
+                        "uri": "/*",
+                        "service_id": "1"
+                    }
+                ]]
+            ))
+            ngx.sleep(0.5)
+
+            local http = require "resty.http"
+            local httpc = http.new()
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
+            local res, err = httpc:request_uri(uri)
+            ngx.status = res.status
+            ngx.log(ngx.WARN, require("inspect")(res))

Review Comment:
   Yes, it does not affect the test correctness.



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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] leslie-tsang commented on pull request #9534: feat: add service path_prefix

Posted by "leslie-tsang (via GitHub)" <gi...@apache.org>.
leslie-tsang commented on PR #9534:
URL: https://github.com/apache/apisix/pull/9534#issuecomment-1562663781

   Need to discuss 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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] monkeyDluffy6017 commented on a diff in pull request #9534: feat: add service path_prefix

Posted by "monkeyDluffy6017 (via GitHub)" <gi...@apache.org>.
monkeyDluffy6017 commented on code in PR #9534:
URL: https://github.com/apache/apisix/pull/9534#discussion_r1205201910


##########
docs/en/latest/admin-api.md:
##########
@@ -585,6 +586,7 @@ Service resource request address: /apisix/admin/services/{id}
 | desc             | False    | Auxiliary   | Description of usage scenarios.                                                                                    | service xxxx                                     |
 | labels           | False    | Match Rules | Attributes of the Service specified as key-value pairs.                                                            | {"version":"v2","build":"16","env":"production"} |
 | enable_websocket | False    | Auxiliary   | Enables a websocket. Set to `false` by default.                                                                    |                                                  |
+| path_prefix | False    | String   | Add prefix to uri.                                                                    |                                                  |

Review Comment:
   Need to update the chinese doc



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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #9534: feat: add service path_prefix

Posted by "kingluo (via GitHub)" <gi...@apache.org>.
kingluo commented on code in PR #9534:
URL: https://github.com/apache/apisix/pull/9534#discussion_r1205310582


##########
apisix/http/route.lua:
##########
@@ -31,6 +31,23 @@ local loadstring = loadstring
 local _M = {}
 
 
+function _M.prefix_uris(route, service)
+    if service and service.value.path_prefix then
+        local uri, uris
+        if route.value.uris then
+            uris = core.table.new(#route.value.uris)
+            for _, uri in ipairs(route.value.uris) do
+                core.table.insert(uris, service.value.path_prefix .. uri)

Review Comment:
   That depends on the validation requirements.
   Not only the trailing slash, the slash, and other invalid chars may exist in any position of the string. So if we need real validation, we need to define its requirement. In fact, the same requirement is needed for proxy-rewrite plugin.
   That is, is it enough if I just remove the trailing slash if exists?



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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] monkeyDluffy6017 commented on a diff in pull request #9534: feat: add service path_prefix

Posted by "monkeyDluffy6017 (via GitHub)" <gi...@apache.org>.
monkeyDluffy6017 commented on code in PR #9534:
URL: https://github.com/apache/apisix/pull/9534#discussion_r1203513978


##########
t/node/service-path-prefix.t:
##########
@@ -0,0 +1,78 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+log_level('warn');
+no_root_location();
+no_shuffle();
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: test service path-prefix
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+
+            assert(t.test('/apisix/admin/services/1',
+                ngx.HTTP_PUT,
+                [[
+                    {
+                        "plugins": {
+                            "proxy-rewrite": {
+                                "regex_uri": ["^/foo/(.*)","/$1"]
+                            }
+                        },
+                        "path_prefix": "/foo",
+                        "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                        }
+                    }
+                ]]
+            ))
+            ngx.sleep(0.5)
+
+            assert(t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[
+                    {
+                        "uri": "/*",
+                        "service_id": "1"
+                    }
+                ]]
+            ))
+            ngx.sleep(0.5)
+
+            local http = require "resty.http"
+            local httpc = http.new()
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
+            local res, err = httpc:request_uri(uri)
+            ngx.status = res.status
+            ngx.log(ngx.WARN, require("inspect")(res))

Review Comment:
   Is this for debug?



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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] monkeyDluffy6017 commented on a diff in pull request #9534: feat: add service path_prefix

Posted by "monkeyDluffy6017 (via GitHub)" <gi...@apache.org>.
monkeyDluffy6017 commented on code in PR #9534:
URL: https://github.com/apache/apisix/pull/9534#discussion_r1203533827


##########
t/node/service-path-prefix.t:
##########
@@ -0,0 +1,78 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+log_level('warn');
+no_root_location();
+no_shuffle();
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: test service path-prefix
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+
+            assert(t.test('/apisix/admin/services/1',
+                ngx.HTTP_PUT,
+                [[
+                    {
+                        "plugins": {
+                            "proxy-rewrite": {
+                                "regex_uri": ["^/foo/(.*)","/$1"]
+                            }
+                        },
+                        "path_prefix": "/foo",
+                        "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                        }
+                    }
+                ]]
+            ))
+            ngx.sleep(0.5)
+
+            assert(t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[
+                    {
+                        "uri": "/*",
+                        "service_id": "1"
+                    }
+                ]]
+            ))
+            ngx.sleep(0.5)
+
+            local http = require "resty.http"
+            local httpc = http.new()
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
+            local res, err = httpc:request_uri(uri)
+            ngx.status = res.status
+            ngx.log(ngx.WARN, require("inspect")(res))

Review Comment:
   Remove it if you don't need 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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #9534: feat: add service path_prefix

Posted by "kingluo (via GitHub)" <gi...@apache.org>.
kingluo commented on code in PR #9534:
URL: https://github.com/apache/apisix/pull/9534#discussion_r1205305949


##########
apisix/init.lua:
##########
@@ -640,6 +641,14 @@ function _M.http_access_phase()
             enable_websocket = service.value.enable_websocket
         end
 
+        if route.value.strip_path_prefix and service.value.path_prefix then
+            local uri = api_ctx.var.uri
+            if uri:find(service.value.path_prefix) == 1 then

Review Comment:
   lua built-in string.find is more simple and high efficient (core.string.has_prefix uses ffi to call memcmp).



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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #9534: feat: add service path_prefix

Posted by "kingluo (via GitHub)" <gi...@apache.org>.
kingluo commented on code in PR #9534:
URL: https://github.com/apache/apisix/pull/9534#discussion_r1205305949


##########
apisix/init.lua:
##########
@@ -640,6 +641,14 @@ function _M.http_access_phase()
             enable_websocket = service.value.enable_websocket
         end
 
+        if route.value.strip_path_prefix and service.value.path_prefix then
+            local uri = api_ctx.var.uri
+            if uri:find(service.value.path_prefix) == 1 then

Review Comment:
   lua built-in string.find is more simple and high efficient.



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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] leslie-tsang closed pull request #9534: feat: add service path_prefix

Posted by "leslie-tsang (via GitHub)" <gi...@apache.org>.
leslie-tsang closed pull request #9534: feat: add service path_prefix
URL: https://github.com/apache/apisix/pull/9534


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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] tokers commented on a diff in pull request #9534: feat: add service path_prefix

Posted by "tokers (via GitHub)" <gi...@apache.org>.
tokers commented on code in PR #9534:
URL: https://github.com/apache/apisix/pull/9534#discussion_r1205147141


##########
apisix/http/route.lua:
##########
@@ -31,6 +31,23 @@ local loadstring = loadstring
 local _M = {}
 
 
+function _M.prefix_uris(route, service)
+    if service and service.value.path_prefix then
+        local uri, uris
+        if route.value.uris then
+            uris = core.table.new(#route.value.uris)
+            for _, uri in ipairs(route.value.uris) do
+                core.table.insert(uris, service.value.path_prefix .. uri)

Review Comment:
   A tiny detail: what if the path_prefix is ended with `/`, in such a case, we'll have continuous slashes. E.g., `/api/v1//order`. While this affects the route match?



##########
apisix/init.lua:
##########
@@ -640,6 +641,14 @@ function _M.http_access_phase()
             enable_websocket = service.value.enable_websocket
         end
 
+        if route.value.strip_path_prefix and service.value.path_prefix then
+            local uri = api_ctx.var.uri
+            if uri:find(service.value.path_prefix) == 1 then

Review Comment:
   Why not using `core.string.has_prefix`?



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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #9534: feat: add service path_prefix

Posted by "kingluo (via GitHub)" <gi...@apache.org>.
kingluo commented on code in PR #9534:
URL: https://github.com/apache/apisix/pull/9534#discussion_r1205310582


##########
apisix/http/route.lua:
##########
@@ -31,6 +31,23 @@ local loadstring = loadstring
 local _M = {}
 
 
+function _M.prefix_uris(route, service)
+    if service and service.value.path_prefix then
+        local uri, uris
+        if route.value.uris then
+            uris = core.table.new(#route.value.uris)
+            for _, uri in ipairs(route.value.uris) do
+                core.table.insert(uris, service.value.path_prefix .. uri)

Review Comment:
   That depends on the validation requirements. For now, I just assume the input is valid.
   Not only the trailing slash, the slash, and other invalid chars may exist in any position of the string. So if we need real validation, we need to define its requirement. In fact, the same requirement is needed for proxy-rewrite plugin.
   That is, is it enough if I just remove the trailing slash if exists?



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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] kingluo commented on a diff in pull request #9534: feat: add service path_prefix

Posted by "kingluo (via GitHub)" <gi...@apache.org>.
kingluo commented on code in PR #9534:
URL: https://github.com/apache/apisix/pull/9534#discussion_r1205310582


##########
apisix/http/route.lua:
##########
@@ -31,6 +31,23 @@ local loadstring = loadstring
 local _M = {}
 
 
+function _M.prefix_uris(route, service)
+    if service and service.value.path_prefix then
+        local uri, uris
+        if route.value.uris then
+            uris = core.table.new(#route.value.uris)
+            for _, uri in ipairs(route.value.uris) do
+                core.table.insert(uris, service.value.path_prefix .. uri)

Review Comment:
   That depends on the validation requirements.
   Not only the trailing slash, the slash, and other invalid chars may exist in any position of the string. So if we need real validation, we need to define its requirement.
   That is, is it enough if I just remove the trailing slash if exists?



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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org