-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathanalysis.lua
49 lines (44 loc) · 1.12 KB
/
analysis.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
local utils = require "utils"
local analysis = ngx.shared.analysis
local function add()
local data = utils.read_data()
local result = {}
local hosts = data['hosts']
if not hosts then
ngx.exit(ngx.HTTP_BAD_REQUEST)
end
for i = 1, #hosts do
local succ, err, _ = analysis:add(hosts[i], 1)
if not succ then
result[hosts[i]] = err
else
result[hosts[i]] = 'ok'
end
end
ngx.say(cjson.encode(result))
ngx.exit(ngx.HTTP_OK)
end
local function delete()
local data = utils.read_data()
analysis:delete(data["host"])
-- TODO check err
ngx.say(cjson.encode({msg = 'ok'}))
ngx.exit(ngx.HTTP_OK)
end
local function get()
local result = {}
local keys = analysis:get_keys(0)
for _, domain in ipairs(keys) do
result[domain] = analysis:get(domain)
end
ngx.say(cjson.encode(result))
ngx.exit(ngx.HTTP_OK)
end
if ngx.var.request_method == 'PUT' then
add()
elseif ngx.var.request_method == 'DELETE' then
delete()
elseif ngx.var.request_method == 'GET' then
get()
end
ngx.exit(ngx.HTTP_BAD_REQUEST)