From d7d12dc8b03ff307838542af88855bb6fc6bab62 Mon Sep 17 00:00:00 2001 From: leslie Date: Mon, 7 Feb 2022 02:47:33 +0800 Subject: [PATCH 1/2] feat: add custom plugin --- apisix/plugins/demo.lua | 96 ++++++++--------------------------------- t/demo/demo.t | 32 ++++++-------- 2 files changed, 29 insertions(+), 99 deletions(-) diff --git a/apisix/plugins/demo.lua b/apisix/plugins/demo.lua index 8ca3d02..8dd2d95 100644 --- a/apisix/plugins/demo.lua +++ b/apisix/plugins/demo.lua @@ -1,22 +1,29 @@ -- local common libs local require = require local core = require("apisix.core") +local ngx = ngx -- local function -- module define +local plugin_sharedict = ngx.shared["demo_dict"] + local plugin_name = "demo" -- plugin schema local plugin_schema = { type = "object", properties = { - body = { - description = "body to replace response.", - type = "string" + msg = { + type = "string", }, + ttl = { + type = "integer", + default = 60, + minumum = 1, + } }, - required = {"body"}, + required = {"msg"}, } local _M = { @@ -27,12 +34,6 @@ local _M = { } --- module interface for init phase -function _M.init() - -end - - -- module interface for schema check -- @param `conf` user defined conf data -- @param `schema_type` defined in `apisix/core/schema.lua` @@ -42,84 +43,21 @@ function _M.check_schema(conf, schema_type) end --- module interface for rewrite phase --- not actually rewrite, just before framework's access phase -function _M.rewrite() - -end - - -- module interface for access phase -- @param `conf` -- @param `ctx` -- @return function _M.access(conf, ctx) - return 200, { message = conf.body } -end - - --- module interface for access phase -function _M.before_proxy(conf, ctx) - -end - - --- module interface for header_filter phase -function _M.header_filter(conf, ctx) - -end - + local val, err = plugin_sharedict:incr(plugin_name, 1, 0, conf.ttl) --- module interface for body_filter phase -function _M.body_filter(conf, ctx) - -end - - --- module interface for log phase --- @param `conf` --- @param `api_ctx` -function _M.log(conf, ctx) - -end - - -local function public_api() - return 200, {msg = "public_api"} -end - - --- module interface for export public api -function _M.api() - return { - { - methods = {"GET"}, - uri = "/apisix/plugin/demo/public_api", - handler = public_api, - } - } -end - - -local function control_api() - local args = ngx.req.get_uri_args() - if args["json"] then - return 200, {msg = "hello"} - else - return 200, "world" + if err then + core.log.error("failed to exec sharedict op, err: ", err) + return 500, { err = err } end -end - -function _M.control_api() - return { - { - methods = {"GET"}, - uris = {"/v1/plugin/demo/control_api"}, - handler = control_api, - } - } + return 200, { message = conf.msg , count = val} end + -- return _M diff --git a/t/demo/demo.t b/t/demo/demo.t index 55bd01d..9442500 100644 --- a/t/demo/demo.t +++ b/t/demo/demo.t @@ -7,10 +7,18 @@ no_root_location(); add_block_preprocessor(sub { my ($block) = @_; + # setup plugin sharedict + my $extra_http_config = $block->http_config // ''; + $extra_http_config .= <<_EOC_; + lua_shared_dict demo_dict 10m; +_EOC_ + + $block->set_value("http_config", $extra_http_config); + # setup default conf.yaml my $extra_yaml_config = $block->extra_yaml_config // <<_EOC_; plugins: - - demo + - demo _EOC_ $block->set_value("extra_yaml_config", $extra_yaml_config); @@ -52,7 +60,7 @@ hello, world! [[{ "plugins": { "demo": { - "body": "test" + "msg": "test" } }, "upstream": { @@ -77,23 +85,7 @@ passed === TEST 3: verify demo access logic ---- request -GET /demo ---- response_body -{"message":"test"} - - - -=== TEST 4: test public api ---- request -GET /apisix/plugin/demo/public_api ---- response_body -{"msg":"public_api"} - - - -=== TEST 5: test control api --- pipelined_requests eval -["GET /v1/plugin/demo/control_api?json=test", "GET /v1/plugin/demo/control_api"] +["GET /demo", "GET /demo"] --- response_body eval -["{\"msg\":\"hello\"}\n", "world"] +[qr/\"count\":1/, qr/\"count\":2/] From 62272cc2277b2599d3adbaa5e5c5ce6b41873fa7 Mon Sep 17 00:00:00 2001 From: leslie Date: Tue, 8 Feb 2022 17:47:09 +0800 Subject: [PATCH 2/2] chore: should use 503 instead of 500 with error msg --- apisix/plugins/demo.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/plugins/demo.lua b/apisix/plugins/demo.lua index 8dd2d95..3ce3e04 100644 --- a/apisix/plugins/demo.lua +++ b/apisix/plugins/demo.lua @@ -52,7 +52,7 @@ function _M.access(conf, ctx) if err then core.log.error("failed to exec sharedict op, err: ", err) - return 500, { err = err } + return 503, { err = err } end return 200, { message = conf.msg , count = val}