Skip to content
Merged
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
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

### help: Show Makefile rules.
.PHONY: help
help:
@echo Makefile rules:
@echo
@grep -E '^### [-A-Za-z0-9_]+:' Makefile | sed 's/###/ /'


### run: Start the apimeta server
.PHONY: run
run:
mkdir -p logs
sudo $$(which openresty) -p $$PWD/


### stop: Stop the apimeta server
.PHONY: stop
stop:
sudo $$(which openresty) -p $$PWD/ -s stop


### clean: Remove generated files
.PHONY: clean
clean:
rm -rf logs/


### reload: Reload the apimeta server
.PHONY: reload
reload:
sudo $$(which openresty) -p $$PWD/ -s reload
Binary file modified doc/apimeta-plugin-design.graffle
Binary file not shown.
2 changes: 2 additions & 0 deletions lua/apimeta/core/resp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local ngx_say = ngx.say
local ngx_exit = ngx.exit

return function (code, body)
code = code or 200

if not body then
ngx_exit(code)
return
Expand Down
22 changes: 22 additions & 0 deletions lua/apimeta/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,27 @@ function _M.load()
return plugins
end

-- user_config = {
-- example_plugin = {i = 1, s = "s", t = {1, 2}},
-- invalid_plugin = {a = "a"},
-- }

function _M.filter_plugin(user_plugins, local_supported_plugins)
-- todo: reuse table
local plugins = new_tab(#local_supported_plugins * 2, 0)

for _, plugin_obj in ipairs(local_supported_plugins) do
local name = plugin_obj.name
local plugin_conf = user_plugins[name]

if plugin_conf then
insert_tab(plugins, plugin_obj)
insert_tab(plugins, plugin_conf)
end
end

return plugins
end


return _M
3 changes: 1 addition & 2 deletions lua/apimeta/route/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local ngx = ngx
local ipairs = ipairs
local r3router = require("resty.r3")
local log = require("apimeta.core.log")
local insert_tab = table.insert
local new_tab = require("table.new")

local router
Expand Down Expand Up @@ -32,7 +31,7 @@ function _M.get_router()
end


local function run_route(params, route, api_ctx)
local function run_route(matched_params, route, api_ctx)
ngx.say("run route id: ", route.id, " host: ", api_ctx.host)
end

Expand Down
32 changes: 32 additions & 0 deletions t/example-plugin.t
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,35 @@ plugin name: example_plugin priority: 1000
--- error_log
failed to load plugin not_exist_plugin err: module 'apimeta.plugins.not_exist_plugin' not found
rewrite(): plugin rewrite phase



=== TEST 3: filter plugins
--- config
location /t {
content_by_lua_block {
local plugin = require("apimeta.plugin")

local all_plugins, err = plugin.load()
if not all_plugins then
ngx.say("failed to load plugins: ", err)
end

local filter_plugins = plugin.filter_plugin({
example_plugin = {i = 1, s = "s", t = {1, 2}},
new_plugin = {a = "a"},
}, all_plugins)

local encode_json = require "cjson.safe" .encode
for i = 1, #filter_plugins, 2 do
local plugin = filter_plugins[i]
local plugin_conf = filter_plugins[i + 1]
ngx.say("plugin [", plugin.name, "] config: ",
encode_json(plugin_conf))
end
}
}
--- request
GET /t
--- response_body
plugin [example_plugin] config: {"i":1,"s":"s","t":[1,2]}