Skip to content

Commit

Permalink
Extract MappingRule from Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Feb 2, 2018
1 parent 91a2882 commit 6cf6e0d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 64 deletions.
66 changes: 2 additions & 64 deletions gateway/src/apicast/configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ local _M = {
local len = string.len
local pairs = pairs
local type = type
local error = error
local tostring = tostring
local next = next
local lower = string.lower
local insert = table.insert
local setmetatable = setmetatable
local re_match = ngx.re.match

local inspect = require 'inspect'
local re = require 'ngx.re'
local env = require 'resty.env'
local resty_url = require 'resty.url'
local util = require 'apicast.util'
local policy_chain = require 'apicast.policy_chain'
local mapping_rule = require 'apicast.mapping_rule'

local mt = { __index = _M, __tostring = function() return 'Configuration' end }

Expand All @@ -30,54 +28,6 @@ local function map(func, tbl)
return newtbl
end

local function regexpify(path)
return path:gsub('?.*', ''):gsub("{.-}", '([\\w_.-]+)'):gsub("%.", "\\.")
end

local regex_variable = '\\{[-\\w_]+\\}'

local function hash_to_array(hash)
local array = {}
for k,v in pairs(hash or {}) do
insert(array, { k, v })
end
return array
end

local function check_querystring_params(params, args)
local match = true

for i=1, #params do
local param = params[i][1]
local expected = params[i][2]
local m, err = re_match(expected, regex_variable, 'oj')
local value = args[param]

if m then
if not value then -- regex variable have to have some value
ngx.log(ngx.DEBUG, 'check query params ', param, ' value missing ', expected)
match = false
break
end
else
if err then ngx.log(ngx.ERR, 'check match error ', err) end

-- if many values were passed use the last one
if type(value) == 'table' then
value = value[#value]
end

if value ~= expected then -- normal variables have to have exact value
ngx.log(ngx.DEBUG, 'check query params does not match ', param, ' value ' , value, ' == ', expected)
match = false
break
end
end
end

return match
end

local Service = require 'apicast.configuration.service'

local noop = function() end
Expand Down Expand Up @@ -159,19 +109,7 @@ function _M.parse_service(service)
app_key = lower(proxy.auth_app_key or 'app_key') -- TODO: use App-Key if location is headers
},
rules = map(function(proxy_rule)
local querystring_parameters = hash_to_array(proxy_rule.querystring_parameters)

return {
method = proxy_rule.http_method,
pattern = proxy_rule.pattern,
regexpified_pattern = regexpify(proxy_rule.pattern),
parameters = proxy_rule.parameters,
querystring_params = function(args)
return check_querystring_params(querystring_parameters, args)
end,
system_name = proxy_rule.metric_system_name or error('missing metric name of rule ' .. inspect(proxy_rule)),
delta = proxy_rule.delta
}
return mapping_rule.from_proxy_rule(proxy_rule)
end, proxy.proxy_rules or {}),

-- I'm not happy about this, but we need a way how to serialize back the object for the management API.
Expand Down
94 changes: 94 additions & 0 deletions gateway/src/apicast/mapping_rule.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
local setmetatable = setmetatable
local pairs = pairs
local error = error
local type = type
local re_match = ngx.re.match
local insert = table.insert

local _M = {}

local mt = { __index = _M }

local function hash_to_array(hash)
local array = {}

for k,v in pairs(hash or {}) do
insert(array, { k, v })
end

return array
end

local function regexpify(path)
return path:gsub('?.*', ''):gsub("{.-}", '([\\w_.-]+)'):gsub("%.", "\\.")
end

local regex_variable = '\\{[-\\w_]+\\}'

local function check_querystring_params(params, args)
local match = true

for i=1, #params do
local param = params[i][1]
local expected = params[i][2]
local m, err = re_match(expected, regex_variable, 'oj')
local value = args[param]

if m then
if not value then -- regex variable have to have some value
ngx.log(ngx.DEBUG, 'check query params ', param,
' value missing ', expected)
match = false
break
end
else
if err then ngx.log(ngx.ERR, 'check match error ', err) end

-- if many values were passed use the last one
if type(value) == 'table' then
value = value[#value]
end

if value ~= expected then -- normal variables have to have exact value
ngx.log(ngx.DEBUG, 'check query params does not match ',
param, ' value ' , value, ' == ', expected)
match = false
break
end
end
end

return match
end

function _M.new(http_method, pattern, params, querystring_params, metric, delta)
local self = setmetatable({}, mt)

local querystring_parameters = hash_to_array(querystring_params)

self.method = http_method
self.pattern = pattern
self.regexpified_pattern = regexpify(pattern)
self.parameters = params
self.system_name = metric or error('missing metric name of rule')
self.delta = delta

self.querystring_params = function(args)
return check_querystring_params(querystring_parameters, args)
end

return self
end

function _M.from_proxy_rule(proxy_rule)
return _M.new(
proxy_rule.http_method,
proxy_rule.pattern,
proxy_rule.parameters,
proxy_rule.querystring_parameters,
proxy_rule.metric_system_name,
proxy_rule.delta
)
end

return _M

0 comments on commit 6cf6e0d

Please sign in to comment.