This is an automated email from the ASF dual-hosted git repository.

spacewander 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 005a790  feat(serverless): pass conf & ctx to the functions (#3971)
005a790 is described below

commit 005a790a5e4a164a9fd51f5e39abf0cd5a1af2c4
Author: 罗泽轩 <[email protected]>
AuthorDate: Thu Apr 8 18:29:32 2021 +0800

    feat(serverless): pass conf & ctx to the functions (#3971)
---
 apisix/plugins/serverless/init.lua   |  2 +-
 docs/en/latest/plugins/serverless.md | 10 +++++--
 docs/zh/latest/plugins/serverless.md | 10 +++++--
 t/plugin/serverless.t                | 51 ++++++++++++++++++++++++++++++++++++
 4 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/apisix/plugins/serverless/init.lua 
b/apisix/plugins/serverless/init.lua
index 634825f..253a4ac 100644
--- a/apisix/plugins/serverless/init.lua
+++ b/apisix/plugins/serverless/init.lua
@@ -75,7 +75,7 @@ return function(plugin_name, priority)
                                                    load_funcs, conf.functions)
 
         for _, func in ipairs(functions) do
-            func()
+            func(conf, ctx)
         end
     end
 
diff --git a/docs/en/latest/plugins/serverless.md 
b/docs/en/latest/plugins/serverless.md
index 6983581..7cde764 100644
--- a/docs/en/latest/plugins/serverless.md
+++ b/docs/en/latest/plugins/serverless.md
@@ -69,6 +69,8 @@ local count = 1
 ngx.say(count)
 ```
 
+Since `v2.6`, we pass the `conf` and `ctx` as the first two arguments to the 
servelss function, like a regular plugin.
+
 ## How To Enable
 
 Here's an example, enable the serverless plugin on the specified route:
@@ -81,7 +83,11 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1  -H 
'X-API-KEY: edd1c9f03433
         "serverless-pre-function": {
             "phase": "rewrite",
             "functions" : ["return function() ngx.log(ngx.ERR, \"serverless 
pre function\"); end"]
-        }
+        },
+        "serverless-post-function": {
+            "phase": "rewrite",
+            "functions" : ["return function(conf, ctx) ngx.log(ngx.ERR, 
\"match uri \", ctx.curr_req_matched and ctx.curr_req_matched._path); end"]
+        },
     },
     "upstream": {
         "type": "roundrobin",
@@ -100,7 +106,7 @@ Use curl to access:
 curl -i http://127.0.0.1:9080/index.html
 ```
 
-Then you will find the message 'serverless pre-function' in the error.log,
+Then you will find the message 'serverless pre-function' and 'match uri 
/index.html' in the error.log,
 which indicates that the specified function is in effect.
 
 ## Disable Plugin
diff --git a/docs/zh/latest/plugins/serverless.md 
b/docs/zh/latest/plugins/serverless.md
index 52b74ca..a1d69cc 100644
--- a/docs/zh/latest/plugins/serverless.md
+++ b/docs/zh/latest/plugins/serverless.md
@@ -58,6 +58,8 @@ local count = 1
 ngx.say(count)
 ```
 
+从 `v2.6` 版本开始,我们会把 `conf` 和 `ctx` 作为头两个参数传递给 serverless 函数,就跟一般的插件一样。
+
 ## 示例
 
 ### 启动插件
@@ -72,7 +74,11 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1  -H 
'X-API-KEY: edd1c9f03433
         "serverless-pre-function": {
             "phase": "rewrite",
             "functions" : ["return function() ngx.log(ngx.ERR, \"serverless 
pre function\"); end"]
-        }
+        },
+        "serverless-post-function": {
+            "phase": "rewrite",
+            "functions" : ["return function(conf, ctx) ngx.log(ngx.ERR, 
\"match uri \", ctx.curr_req_matched and ctx.curr_req_matched._path); end"]
+        },
     },
     "upstream": {
         "type": "roundrobin",
@@ -91,7 +97,7 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1  -H 
'X-API-KEY: edd1c9f03433
 curl -i http://127.0.0.1:9080/index.html
 ```
 
-然后你在 error.log 日志中就会发现 `serverless pre function` 这个 error 级别的日志,
+然后你在 error.log 日志中就会发现 `serverless pre function` 和 `match uri /index.html` 两个 
error 级别的日志,
 表示指定的函数已经生效。
 
 ### 移除插件
diff --git a/t/plugin/serverless.t b/t/plugin/serverless.t
index 3f3d3a3..2d78390 100644
--- a/t/plugin/serverless.t
+++ b/t/plugin/serverless.t
@@ -628,3 +628,54 @@ Host: foo.com
 --- error_code: 301
 --- response_headers
 Location: https://foo.com/hello
+
+
+
+=== TEST 21: access conf & ctx in serverless
+--- 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,
+                 [[{
+                    "plugins": {
+                        "serverless-post-function": {
+                        "functions" : ["return function(conf, ctx) 
ngx.log(ngx.WARN, 'default phase: ', conf.phase);
+                                       ngx.log(ngx.WARN, 'match uri ', 
ctx.curr_req_matched and ctx.curr_req_matched._path);
+                                       ctx.var.upstream_uri = '/server_port' 
end"]
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/hello"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 22: check plugin
+--- request
+GET /hello
+--- response_body chomp
+1980
+--- error_log
+default phase: access
+match uri /hello

Reply via email to