diff --git a/apicast/src/executor.lua b/apicast/src/executor.lua index 9ee3de840..300ca6ad9 100644 --- a/apicast/src/executor.lua +++ b/apicast/src/executor.lua @@ -1,18 +1,23 @@ -local setmetatable = setmetatable +local resty_env = require('resty.env') + +if resty_env.get('APICAST_MODULE') then + return require('module') +end local policy_chain = require('policy_chain') +local policy = require('policy') local linked_list = require('linked_list') +local setmetatable = setmetatable + local _M = { } local mt = { __index = _M } -- forward all policy methods to the policy chain -for i=1, #(policy_chain.PHASES) do - local phase_name = policy_chain.PHASES[i] - - _M[phase_name] = function(self, ...) - return self.policy_chain[phase_name](self.policy_chain, self:context(phase_name), ...) +for _,phase in policy:phases() do + _M[phase] = function(self, ...) + return self.policy_chain[phase](self.policy_chain, self:context(phase), ...) end end diff --git a/apicast/src/policy.lua b/apicast/src/policy.lua index 15c7fa122..663f2dc43 100644 --- a/apicast/src/policy.lua +++ b/apicast/src/policy.lua @@ -1,8 +1,14 @@ -local _M = {} +local _M = { + PHASES = { + 'init', 'init_worker', + 'rewrite', 'access', 'balancer', + 'header_filter', 'body_filter', + 'post_action', 'log' + } +} local setmetatable = setmetatable local ipairs = ipairs -local policy_chain = require('policy_chain') local noop = function() end @@ -22,15 +28,15 @@ function _M.new(name, version) return setmetatable({}, mt) end - for _, phase in _M.phases() do + for _, phase in _M:phases() do policy[phase] = noop end return policy end -function _M.phases() - return ipairs(policy_chain.PHASES) +function _M:phases() + return ipairs(self.PHASES) end return _M diff --git a/apicast/src/policy/local_chain.lua b/apicast/src/policy/local_chain.lua index cafa4aa6b..62ba651f3 100644 --- a/apicast/src/policy/local_chain.lua +++ b/apicast/src/policy/local_chain.lua @@ -16,7 +16,7 @@ local function build_chain(context) end -- forward all policy methods to the policy chain -for _, phase in policy.phases() do +for _, phase in policy:phases() do _M[phase] = function(_, context, ...) local policy_chain = find_policy_chain(context) diff --git a/apicast/src/policy_chain.lua b/apicast/src/policy_chain.lua index e90a9a54e..4df203447 100644 --- a/apicast/src/policy_chain.lua +++ b/apicast/src/policy_chain.lua @@ -1,9 +1,3 @@ -local resty_env = require('resty.env') - -if resty_env.get('APICAST_MODULE') then - return require('module') -end - local setmetatable = setmetatable local insert = table.insert local error = error @@ -13,14 +7,10 @@ local require = require local noop = function() end local linked_list = require('linked_list') +local policy = require('policy') local _M = { - PHASES = { - 'init', 'init_worker', - 'rewrite', 'access', 'balancer', - 'header_filter', 'body_filter', - 'post_action', 'log' - } + } local mt = { @@ -92,10 +82,8 @@ local function call_chain(phase_name) end end -for i=1, #(_M.PHASES) do - local phase_name = _M.PHASES[i] - - _M[phase_name] = call_chain(phase_name) +for _,phase in policy:phases() do + _M[phase] = call_chain(phase) end return _M.build()