/
upstream_selector.lua
56 lines (41 loc) · 1.48 KB
/
upstream_selector.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local ipairs = ipairs
local setmetatable = setmetatable
local Upstream = require('apicast.upstream')
local _M = {}
local mt = { __index = _M }
function _M.new()
local self = setmetatable({}, mt)
return self
end
--- Selects an upstream based on a list of rules.
-- @tparam rules table Table with instances of Rule
-- @tparam context table Context used to evaluate the conditions
-- @treturn Upstream Returns an instance of Upstream initialized with the url
-- of the first rule whose condition evaluates to true. If there are no rules
-- or none of them evaluate to true, this method returns nil.
function _M.select(_, rules, context)
if not rules then return nil end
local service = context.service or ngx.ctx.service or {}
for _, rule in ipairs(rules) do
local cond_is_true = rule.condition:evaluate(context)
if cond_is_true then
ngx.log(ngx.DEBUG, 'Rule with URL ', rule.url, ' matched')
local upstream = Upstream.new(rule.url)
local host_header = rule.host_header or service and service.hostname_rewrite
if host_header and host_header ~= '' then
upstream:use_host_header(host_header)
end
if rule.owner_id then
upstream:set_owner_id(rule.owner_id)
end
if rule.replace_path then
upstream:append_path(rule.replace_path:render(context))
-- Set uri as nil if not will be appended to the upstream
ngx.req.set_uri("/")
end
return upstream
end
end
return nil
end
return _M