-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapibus.lua
60 lines (49 loc) · 1.12 KB
/
apibus.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
-- Api Bus for contact ajax requests
--
-- Author: Michael LYU <[email protected]>
--
-- Date: 2016-01-23
--
-- Version: 1.0.0
local cjson = require "cjson.safe";
ngx.req.read_body();
local post_args = ngx.req.get_post_args();
local bus = cjson.decode(post_args.apibus);
local methods = {
GET = ngx.HTTP_GET,
POST = ngx.HTTP_POST,
PUT = ngx.HTTP_PUT,
DELETE = ngx.HTTP_DELETE
};
local seats = {};
local seat;
local options;
for k, v in pairs(bus) do
seat = {v.url};
options = {};
if v.method then
options['method'] = methods[string.upper(v.method)];
end
if v.params then
options['args'] = v.params;
end
if v.body then
options['body'] = v.body;
end
if table.getn(options) then
table.insert(seat, 2, options);
end
table.insert(seats, k, seat);
end
local responses = {ngx.location.capture_multi(seats)};
local bus = {};
local res;
for k, v in pairs(responses) do
if v.status == 200 then
res = cjson.decode(v.body);
else
res = "error";
end
table.insert(bus, k, res);
end
ngx.say(cjson.encode(bus));