Skip to content

Commit

Permalink
move phases definition to policy
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed Nov 7, 2017
1 parent 9f1e4fc commit 7624cd7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
17 changes: 11 additions & 6 deletions apicast/src/executor.lua
Original file line number Diff line number Diff line change
@@ -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

Expand Down
16 changes: 11 additions & 5 deletions apicast/src/policy.lua
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
2 changes: 1 addition & 1 deletion apicast/src/policy/local_chain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
20 changes: 4 additions & 16 deletions apicast/src/policy_chain.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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()

0 comments on commit 7624cd7

Please sign in to comment.