-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.lua
90 lines (80 loc) · 2.85 KB
/
router.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
--author: Komeil Majidi
package.path = package.path .. ";/www/api/?.lua";
local routes = {
["/api/v1/user/authenticate/"] = {
controller = "Controllers.authenticate_controller",
action = "authenticate_user",
method = "POST"
},
["/api/v1/user/test/"] = {
controller = "Controllers.temp_controller",
action = "temp_script",
filters = { "token_filter" },
method = "POST"
},
["/api/v1/config/firewall/addredirect"] = {
controller = "Controllers.firewall_controller",
action = "add_PortForward",
filters = { "token_filter" },
method = "POST"
},
["/api/v1/config/firewall/removeredirect"] = {
controller = "Controllers.firewall_controller",
action = "remove_PortForward",
filters = { "token_filter" },
method = "POST"
},
["/api/v1/config/firewall/getredirect"] = {
controller = "Controllers.firewall_controller",
action = "get_PortForwards",
filters = { "token_filter" },
method = "GET"
},
["/api/v1/config/firewall/getrezones"] = {
controller = "Controllers.firewall_controller",
action = "get_Zones",
filters = { "token_filter" },
method = "GET"
},
["/api/v1/config/wifi/getdevices"] = {
controller = "Controllers.wifi_controller",
action = "get_WifiConfig",
filters = { "token_filter" },
method = "GET"
},
};
local token = os.getenv("HTTP_TOKEN");
local request_json = io.read("*a");
local path = tostring(os.getenv("REQUEST_URI"));
local method = tostring(os.getenv("REQUEST_METHOD"));
local method_validate = true
for route, routeInfo in pairs(routes) do
local params = { string.match(path, route) };
if #params > 0 then
if routeInfo.method ~= method then
require("Views.json_view");
printResponse("error", "400", "Bad Request: Invalid request method", "application/json", "Bad Request", response);
os.exit(1);
end
if routeInfo.filters and next(routeInfo.filters) then
if token == nil then
require("Views.json_view");
printResponse("error", "403", "Forbidden: Access denied", "application/json", "Forbidden", response);
os.exit(1);
end;
require("Controllers.token_controller");
local token_validate = validate_token(token);
if token_validate == true then
local controller = require(routeInfo.controller);
local response = _G[routeInfo.action](request_json);
else
require("Views.json_view");
printResponse("error", "403", "Forbidden: Access denied", "application/json", "Forbidden", response);
os.exit(1);
end;
else
local controller = require(routeInfo.controller);
local response = _G[routeInfo.action](request_json);
end;
end;
end;