Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 17 additions & 79 deletions apisix/plugins/demo.lua
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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`
Expand All @@ -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 <int, object or string>
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 503, { 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
32 changes: 12 additions & 20 deletions t/demo/demo.t
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -52,7 +60,7 @@ hello, world!
[[{
"plugins": {
"demo": {
"body": "test"
"msg": "test"
}
},
"upstream": {
Expand All @@ -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/]