diff --git a/Makefile b/Makefile new file mode 100644 index 000000000000..b2283233ba71 --- /dev/null +++ b/Makefile @@ -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 diff --git a/doc/apimeta-plugin-design.graffle b/doc/apimeta-plugin-design.graffle index 280a830e28e0..c1cdf5ed3c9f 100644 Binary files a/doc/apimeta-plugin-design.graffle and b/doc/apimeta-plugin-design.graffle differ diff --git a/lua/apimeta/core/resp.lua b/lua/apimeta/core/resp.lua index 15597496eaa7..0ba6fa8d3ff6 100644 --- a/lua/apimeta/core/resp.lua +++ b/lua/apimeta/core/resp.lua @@ -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 diff --git a/lua/apimeta/plugin.lua b/lua/apimeta/plugin.lua index c71bb8b3af8b..cba162891362 100644 --- a/lua/apimeta/plugin.lua +++ b/lua/apimeta/plugin.lua @@ -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 diff --git a/lua/apimeta/route/handler.lua b/lua/apimeta/route/handler.lua index 925f2795a713..3984fe53c5ec 100644 --- a/lua/apimeta/route/handler.lua +++ b/lua/apimeta/route/handler.lua @@ -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 @@ -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 diff --git a/t/example-plugin.t b/t/example-plugin.t index 4ef3286fade2..902b41af2fdc 100644 --- a/t/example-plugin.t +++ b/t/example-plugin.t @@ -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]}