2019-04-10 11:08:25 +08:00
|
|
|
-- Copyright (C) Yuansheng Wang
|
|
|
|
|
2019-04-11 15:00:59 +08:00
|
|
|
local require = require
|
2019-04-11 12:02:16 +08:00
|
|
|
local log = require("apimeta.comm.log")
|
|
|
|
local resp = require("apimeta.comm.resp")
|
|
|
|
local route_handler = require("apimeta.route.handler")
|
2019-04-11 15:00:59 +08:00
|
|
|
local new_tab = require("table.new")
|
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-04-10 11:08:25 +08:00
|
|
|
local _M = {}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
function _M.init_worker()
|
2019-04-11 12:02:16 +08:00
|
|
|
require("apimeta.route.load").init_worker()
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function _M.access()
|
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-04-11 12:02:16 +08:00
|
|
|
if not ok then
|
|
|
|
log.warn("not find any matched route")
|
2019-04-11 15:00:59 +08:00
|
|
|
resp(404)
|
2019-04-11 12:02:16 +08:00
|
|
|
end
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function _M.header_filter()
|
2019-04-11 12:02:16 +08:00
|
|
|
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function _M.log()
|
2019-04-11 12:02:16 +08:00
|
|
|
|
2019-04-10 11:08:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return _M
|