This is an automated email from the ASF dual-hosted git repository. membphis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push: new b1770d4 feat: support to fetch `route_id` and `service_id` via `ctx.var` (#2326) b1770d4 is described below commit b1770d4ca7e4e8d0f92e655d093b9c5563cc9be6 Author: Firstsawyou <52862365+firstsaw...@users.noreply.github.com> AuthorDate: Sun Sep 27 22:29:59 2020 +0800 feat: support to fetch `route_id` and `service_id` via `ctx.var` (#2326) --- apisix/core/ctx.lua | 7 ++ t/core/ctx.t | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua index 0d6e364..7b87e68 100644 --- a/apisix/core/ctx.lua +++ b/apisix/core/ctx.lua @@ -28,6 +28,7 @@ local ngx_var = ngx.var local re_gsub = ngx.re.gsub local type = type local error = error +local ngx = ngx ffi.cdef[[ @@ -89,6 +90,12 @@ do key = re_gsub(key, "-", "_", "jo") val = get_var(key, t._request) + elseif key == "route_id" then + val = ngx.ctx.api_ctx and ngx.ctx.api_ctx.route_id + + elseif key == "service_id" then + val = ngx.ctx.api_ctx and ngx.ctx.api_ctx.service_id + else val = get_var(key, t._request) end diff --git a/t/core/ctx.t b/t/core/ctx.t index cea8ca5..ae02b52 100644 --- a/t/core/ctx.t +++ b/t/core/ctx.t @@ -155,3 +155,190 @@ GET /t?a=aaa --- error_code: 500 --- error_log invalid argument, expect string value + + + +=== TEST 7: add route and get `route_id` +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "methods": ["GET"], + "plugins": { + "serverless-pre-function": { + "phase": "access", + "functions" : ["return function() ngx.log(ngx.INFO, \"route_id: \", ngx.ctx.api_ctx.var.route_id) end"] + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:1980": 1 + } + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 8: `url` exist and `route_id` is 1 +--- request +GET /hello +--- response_body +hello world +--- error_log +route_id: 1 +--- no_error_log +[error] + + + +=== TEST 9: create a service and `service_id` is 1 +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/services/1', + ngx.HTTP_PUT, + [[{ + "desc": "new_service" + }]], + [[{ + "node": { + "value": { + "desc": "new_service" + }, + "key": "/apisix/services/1" + }, + "action": "set" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 10: the route object not bind any service object +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "methods": ["GET"], + "plugins": { + "serverless-pre-function": { + "phase": "access", + "functions" : ["return function() ngx.log(ngx.INFO, \"service_id: \", ngx.ctx.api_ctx.var.service_id or 'empty route_id') end"] + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:1980": 1 + } + }, + "uri": "/hello" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 11: service_id is empty +--- request +GET /hello +--- response_body +hello world +--- error_log +service_id: empty route_id +--- no_error_log +[error] + + + +=== TEST 12: update route and binding service_id +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "methods": ["GET"], + "service_id": 1, + "plugins": { + "serverless-pre-function": { + "phase": "access", + "functions" : ["return function() ngx.log(ngx.INFO, \"service_id: \", ngx.ctx.api_ctx.var.service_id) end"] + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:1980": 1 + } + }, + "uri": "/hello" + }]] + ) + + ngx.status = code + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 13: service_id is 1 +--- request +GET /hello +--- response_body +hello world +--- error_log +service_id: 1 +--- no_error_log +[error]