-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcustom_enabled_routes.lua
72 lines (64 loc) · 2.25 KB
/
custom_enabled_routes.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
local function wildcard_search(text, pat)
local pattern = pat:gsub("*", "\0"):gsub("%p", "%%%0"):gsub("%z", ".-")
if text:find(pattern) ~= nil then
return true
else
return false
end
end
-- file must have 2 fields, separated by a single whitespace.
-- first field is a Method, second field is a Route (could contains wildcard)
local ROUTES_FILE="/tmp/urls.txt"
local REFRESH_SEC = 60
local routes={}
local last_updated = 0
local routes_file_not_found = false
-- return `true` if `ROUTES_FILE` contains a method and a path matching `uri` and request method
-- content from `ROUTES_FILE` file is reloaded every `REFRESH_SEC` seconds
-- if the file doesn't exist: enable for all routes
_M.custom_enabled_routes = function(uri)
-- the file doesn't exist: enable for all routes
if routes_file_not_found == true then
return true
end
-- periodically reload the file
local now = os.time(os.date("!*t"))
if now - last_updated >= REFRESH_SEC then
last_updated = now
-- check if the file exists
local f = io.open(ROUTES_FILE, "r")
if f ~= nil then
io.close(f)
else
-- the file doesn't exist: enable for all routes
ngx.log(ngx.ERR, "Enabled routes file does not exist: " .. ROUTES_FILE)
routes_file_not_found = true
return true
end
ngx.log(ngx.DEBUG, "Reloading enabled routes..")
-- empty routes table
for i, _ in ipairs(routes) do routes[i] = nil end
-- read the file, fill the routes table
for line in io.lines(ROUTES_FILE) do
-- split the line
local words = {}
for w in line:gmatch("%S+") do
table.insert(words, w)
end
if #words >=2 and words[1] ~= nil and words[2] ~= nil then
local val = {}
val["method"] = words[1]:upper()
val["path"] = words[2]
table.insert(routes, val)
end
end
end
-- check for method / path
local method = ngx.req.get_method()
for _, r in ipairs(routes) do
if r["method"] == method and wildcard_search(uri, r["path"]) then
return true
end
end
return false
end