2019-04-10 11:08:25 +08:00
|
|
|
-- Copyright (C) Yuansheng Wang
|
|
|
|
|
2019-04-11 15:00:59 +08:00
|
|
|
local require = require
|
2019-05-20 10:36:23 +08:00
|
|
|
local core = require("apisix.core")
|
2019-05-13 14:28:54 +08:00
|
|
|
local route_handler = require("apisix.route.handler")
|
|
|
|
local base_plugin = require("apisix.base_plugin")
|
2019-04-11 15:00:59 +08:00
|
|
|
local new_tab = require("table.new")
|
2019-05-17 17:29:37 +08:00
|
|
|
local load_balancer = require("apisix.balancer") .run
|
2019-04-11 14:36:53 +08:00
|
|
|
local ngx = ngx
|
2019-04-11 12:02:16 +08:00
|
|
|
local ngx_req = ngx.req
|
2019-04-11 14:34:18 +08:00
|
|
|
local ngx_var = ngx.var
|
2019-04-11 12:02:16 +08:00
|
|
|
|
2019-05-20 10:36:23 +08:00
|
|
|
|
2019-05-17 17:29:37 +08:00
|
|
|
local _M = {version = 0.1}
|
2019-04-10 11:08:25 +08:00
|
|
|
|
2019-05-20 10:36:23 +08:00
|
|
|
|
2019-04-10 11:08:25 +08:00
|
|
|
function _M.init()
|
2019-04-11 15:00:59 +08:00
|
|
|
require("resty.core")
|
|
|
|
require("ngx.re").opt("jit_stack_size", 200 * 1024)
|
|
|
|
require("jit.opt").start("minstitch=2", "maxtrace=4000",
|
|
|
|
"maxrecord=8000", "sizemcode=64",
|
|
|
|
"maxmcode=4000", "maxirconst=1000")
|
2019-05-11 10:14:46 +08:00
|
|
|
|
2019-05-13 14:28:54 +08:00
|
|
|
require("apisix.route.handler").init()
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-05-20 10:36:23 +08:00
|
|
|
|
2019-04-10 11:08:25 +08:00
|
|
|
function _M.init_worker()
|
2019-05-13 14:28:54 +08:00
|
|
|
require("apisix.route.load").init_worker()
|
2019-05-20 10:36:23 +08:00
|
|
|
require("apisix.balancer").init_worker()
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-05-20 10:36:23 +08:00
|
|
|
|
2019-05-08 11:30:16 +08:00
|
|
|
function _M.rewrite_phase()
|
2019-04-11 12:02:16 +08:00
|
|
|
local ngx_ctx = ngx.ctx
|
|
|
|
local api_ctx = ngx_ctx.api_ctx
|
|
|
|
|
|
|
|
if api_ctx == nil then
|
2019-04-11 15:00:59 +08:00
|
|
|
-- todo: reuse this table
|
|
|
|
api_ctx = new_tab(0, 32)
|
2019-04-11 12:02:16 +08:00
|
|
|
ngx_ctx.api_ctx = api_ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
api_ctx.method = api_ctx.method or ngx_req.get_method()
|
2019-04-11 14:34:18 +08:00
|
|
|
api_ctx.uri = api_ctx.uri or ngx_var.uri
|
|
|
|
api_ctx.host = api_ctx.host or ngx_var.host
|
2019-04-11 12:02:16 +08:00
|
|
|
|
2019-04-11 15:00:59 +08:00
|
|
|
local router, dispatch_uri = route_handler.get_router()
|
|
|
|
local ok
|
|
|
|
if dispatch_uri then
|
|
|
|
ok = router:dispatch(api_ctx.method, api_ctx.uri, api_ctx)
|
|
|
|
else
|
|
|
|
ok = router:dispatch(api_ctx.method, api_ctx.host .. api_ctx.uri,
|
|
|
|
api_ctx)
|
|
|
|
end
|
2019-05-08 10:38:45 +08:00
|
|
|
|
2019-04-11 12:02:16 +08:00
|
|
|
if not ok then
|
2019-05-20 10:36:23 +08:00
|
|
|
core.log.info("not find any matched route")
|
|
|
|
return core.resp(404)
|
2019-05-08 10:38:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- todo: move those code to another single file
|
2019-05-17 15:58:15 +08:00
|
|
|
-- todo optimize: cache `local_supported_plugins`
|
|
|
|
local local_supported_plugins, err = base_plugin.load()
|
|
|
|
if not local_supported_plugins then
|
2019-05-08 10:38:45 +08:00
|
|
|
ngx.say("failed to load plugins: ", err)
|
|
|
|
end
|
|
|
|
|
|
|
|
local filter_plugins = base_plugin.filter_plugin(
|
2019-05-17 15:58:15 +08:00
|
|
|
api_ctx.matched_route, local_supported_plugins)
|
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
api_ctx.filter_plugins = filter_plugins
|
2019-05-17 16:23:51 +08:00
|
|
|
api_ctx.conf_version = api_ctx.matched_route.modifiedIndex
|
2019-05-17 15:58:15 +08:00
|
|
|
-- todo: fetch the upstream node status, it may be stored in
|
|
|
|
-- different places.
|
2019-05-08 11:30:16 +08:00
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
for i = 1, #filter_plugins, 2 do
|
|
|
|
local plugin = filter_plugins[i]
|
2019-05-08 11:30:16 +08:00
|
|
|
if plugin.rewrite then
|
2019-05-17 15:58:15 +08:00
|
|
|
plugin.rewrite(filter_plugins[i + 1],
|
2019-05-17 16:23:51 +08:00
|
|
|
api_ctx.conf_version)
|
2019-05-08 10:38:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-08 11:30:16 +08:00
|
|
|
function _M.access_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
local api_ctx = ngx.ctx.api_ctx
|
|
|
|
if not api_ctx.filter_plugins then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local filter_plugins = api_ctx.filter_plugins
|
|
|
|
for i = 1, #filter_plugins, 2 do
|
|
|
|
local plugin = filter_plugins[i]
|
|
|
|
if plugin.access then
|
2019-05-17 15:58:15 +08:00
|
|
|
plugin.access(filter_plugins[i + 1],
|
2019-05-17 16:23:51 +08:00
|
|
|
api_ctx.conf_version)
|
2019-05-08 10:38:45 +08:00
|
|
|
end
|
2019-04-11 12:02:16 +08:00
|
|
|
end
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-05-08 11:30:16 +08:00
|
|
|
function _M.header_filter_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
local api_ctx = ngx.ctx.api_ctx
|
|
|
|
if not api_ctx.filter_plugins then
|
|
|
|
return
|
|
|
|
end
|
2019-04-11 12:02:16 +08:00
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
local filter_plugins = api_ctx.filter_plugins
|
|
|
|
for i = 1, #filter_plugins, 2 do
|
|
|
|
local plugin = filter_plugins[i]
|
|
|
|
if plugin.header_filter then
|
2019-05-17 15:58:15 +08:00
|
|
|
plugin.header_filter(filter_plugins[i + 1],
|
2019-05-17 16:23:51 +08:00
|
|
|
api_ctx.conf_version)
|
2019-05-08 10:38:45 +08:00
|
|
|
end
|
|
|
|
end
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-05-08 11:30:16 +08:00
|
|
|
function _M.log_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
local api_ctx = ngx.ctx.api_ctx
|
|
|
|
if not api_ctx.filter_plugins then
|
|
|
|
return
|
|
|
|
end
|
2019-04-11 12:02:16 +08:00
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
local filter_plugins = api_ctx.filter_plugins
|
|
|
|
for i = 1, #filter_plugins, 2 do
|
|
|
|
local plugin = filter_plugins[i]
|
|
|
|
if plugin.log then
|
2019-05-17 15:58:15 +08:00
|
|
|
plugin.log(filter_plugins[i + 1],
|
2019-05-17 16:23:51 +08:00
|
|
|
api_ctx.conf_version)
|
2019-05-08 10:38:45 +08:00
|
|
|
end
|
|
|
|
end
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-05-17 15:58:15 +08:00
|
|
|
function _M.balancer_phase()
|
|
|
|
local api_ctx = ngx.ctx.api_ctx
|
|
|
|
if not api_ctx.filter_plugins then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: fetch the upstream by upstream_id
|
2019-05-17 16:23:51 +08:00
|
|
|
load_balancer(api_ctx.matched_route, api_ctx.conf_version)
|
2019-05-17 15:58:15 +08:00
|
|
|
end
|
|
|
|
|
2019-04-10 11:08:25 +08:00
|
|
|
return _M
|