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-21 22:53:28 +08:00
|
|
|
local router = require("apisix.route").get
|
2019-05-27 17:14:06 +08:00
|
|
|
local plugin = require("apisix.plugin")
|
2019-05-29 10:29:40 +08:00
|
|
|
local load_balancer = require("apisix.balancer").run
|
2019-05-27 17:14:06 +08:00
|
|
|
local service_fetch = require("apisix.service").get
|
2019-04-11 14:36:53 +08:00
|
|
|
local ngx = ngx
|
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-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-21 22:53:28 +08:00
|
|
|
require("apisix.route").init_worker()
|
2019-05-20 10:36:23 +08:00
|
|
|
require("apisix.balancer").init_worker()
|
2019-05-24 22:38:40 +08:00
|
|
|
require("apisix.plugin").init_worker()
|
2019-05-27 17:14:06 +08:00
|
|
|
require("apisix.service").init_worker()
|
2019-05-23 16:35:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function run_plugin(phase, filter_plugins, api_ctx)
|
|
|
|
api_ctx = api_ctx or ngx.ctx.api_ctx
|
2019-05-23 21:57:51 +08:00
|
|
|
if not api_ctx then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-05-23 16:35:47 +08:00
|
|
|
filter_plugins = filter_plugins or api_ctx.filter_plugins
|
|
|
|
if not filter_plugins then
|
2019-05-29 10:29:40 +08:00
|
|
|
return api_ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
if phase ~= "log" then
|
|
|
|
for i = 1, #filter_plugins, 2 do
|
|
|
|
local phase_fun = filter_plugins[i][phase]
|
|
|
|
if phase_fun then
|
|
|
|
local code, body = phase_fun(filter_plugins[i + 1], api_ctx)
|
|
|
|
if code or body then
|
|
|
|
core.response.exit(code, body)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return api_ctx
|
2019-05-23 16:35:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1, #filter_plugins, 2 do
|
2019-05-27 17:14:06 +08:00
|
|
|
local phase_fun = filter_plugins[i][phase]
|
2019-05-28 10:47:53 +08:00
|
|
|
if phase_fun then
|
2019-05-29 10:29:40 +08:00
|
|
|
phase_fun(filter_plugins[i + 1], api_ctx)
|
2019-05-23 16:35:47 +08:00
|
|
|
end
|
|
|
|
end
|
2019-05-29 10:29:40 +08:00
|
|
|
|
|
|
|
return api_ctx
|
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-05-29 10:29:40 +08:00
|
|
|
api_ctx = core.tablepool.fetch("api_ctx", 0, 32)
|
2019-04-11 12:02:16 +08:00
|
|
|
end
|
|
|
|
|
2019-05-28 10:47:53 +08:00
|
|
|
core.ctx.set_vars_meta(api_ctx)
|
|
|
|
local method = api_ctx.var["method"]
|
|
|
|
local uri = api_ctx.var["uri"]
|
|
|
|
-- local host = api_ctx.var["host"] -- todo: support host
|
2019-05-08 10:38:45 +08:00
|
|
|
|
2019-05-27 17:14:06 +08:00
|
|
|
-- run the api router
|
|
|
|
local api_router = plugin.api_router()
|
2019-05-24 23:11:29 +08:00
|
|
|
if api_router and api_router.dispatch then
|
|
|
|
local ok = api_router:dispatch(method, uri, api_ctx)
|
2019-05-24 22:38:40 +08:00
|
|
|
if ok then
|
2019-05-27 17:14:06 +08:00
|
|
|
-- core.log.warn("finish api route")
|
2019-05-24 22:38:40 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-24 23:11:29 +08:00
|
|
|
ngx_ctx.api_ctx = api_ctx
|
|
|
|
|
2019-05-21 22:53:28 +08:00
|
|
|
local ok = router():dispatch(method, uri, api_ctx)
|
2019-04-11 12:02:16 +08:00
|
|
|
if not ok then
|
2019-05-23 15:19:07 +08:00
|
|
|
core.log.warn("not find any matched route")
|
2019-05-23 21:57:51 +08:00
|
|
|
return core.response.exit(404)
|
2019-05-08 10:38:45 +08:00
|
|
|
end
|
|
|
|
|
2019-05-27 17:14:06 +08:00
|
|
|
-- core.log.warn("route: ",
|
|
|
|
-- core.json.encode(api_ctx.matched_route, true))
|
|
|
|
|
|
|
|
local route = api_ctx.matched_route
|
|
|
|
if route.value.service_id then
|
|
|
|
-- core.log.warn("matched route: ", core.json.encode(route.value))
|
|
|
|
local service = service_fetch(route.value.service_id)
|
|
|
|
local changed
|
|
|
|
route, changed = plugin.merge_service_route(service, route)
|
|
|
|
|
|
|
|
if changed then
|
|
|
|
api_ctx.conf_type = "route&service"
|
|
|
|
api_ctx.conf_version = route.modifiedIndex .. "&"
|
|
|
|
.. service.modifiedIndex
|
|
|
|
api_ctx.conf_id = route.value.id .. "&"
|
|
|
|
.. service.value.id
|
|
|
|
else
|
|
|
|
api_ctx.conf_type = "route"
|
|
|
|
api_ctx.conf_version = route.modifiedIndex
|
|
|
|
api_ctx.conf_id = route.value.id
|
|
|
|
end
|
|
|
|
|
2019-05-20 16:50:43 +08:00
|
|
|
else
|
|
|
|
api_ctx.conf_type = "route"
|
2019-05-27 17:14:06 +08:00
|
|
|
api_ctx.conf_version = route.modifiedIndex
|
|
|
|
api_ctx.conf_id = route.value.id
|
2019-05-20 16:50:43 +08:00
|
|
|
end
|
|
|
|
|
2019-05-27 17:14:06 +08:00
|
|
|
api_ctx.filter_plugins = plugin.filter(route)
|
2019-05-08 11:30:16 +08:00
|
|
|
|
2019-05-27 17:14:06 +08:00
|
|
|
run_plugin("rewrite", api_ctx.filter_plugins, api_ctx)
|
2019-05-08 10:38:45 +08:00
|
|
|
end
|
|
|
|
|
2019-05-08 11:30:16 +08:00
|
|
|
function _M.access_phase()
|
2019-05-23 16:35:47 +08:00
|
|
|
run_plugin("access")
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-05-08 11:30:16 +08:00
|
|
|
function _M.header_filter_phase()
|
2019-05-23 16:35:47 +08:00
|
|
|
run_plugin("header_filter")
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
2019-05-08 11:30:16 +08:00
|
|
|
function _M.log_phase()
|
2019-05-29 10:29:40 +08:00
|
|
|
local api_ctx = run_plugin("log")
|
|
|
|
if api_ctx then
|
|
|
|
core.ctx.release_vars(api_ctx)
|
|
|
|
core.tablepool.release("api_ctx", api_ctx)
|
|
|
|
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
|
2019-05-23 21:57:51 +08:00
|
|
|
if not api_ctx or not api_ctx.filter_plugins then
|
2019-05-17 15:58:15 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: fetch the upstream by upstream_id
|
2019-05-23 22:38:08 +08:00
|
|
|
load_balancer(api_ctx.matched_route, api_ctx)
|
2019-05-17 15:58:15 +08:00
|
|
|
end
|
|
|
|
|
2019-04-10 11:08:25 +08:00
|
|
|
return _M
|