-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.lua
executable file
·84 lines (63 loc) · 2.23 KB
/
http.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
_G.core = require("core")
_G.http = require('http')
_G.https = require('https')
_G.json = require('json')
_G.config = require('./config')
_G.corohttp = require('coro-http')
local Emitter = core.Emitter
local listen_ip = config.get("listen_ip")
local listen_port = config.get("listen_port")
local tokens = config.get("tokens")
local http_emitter = Emitter:new()
listen_port = listen_port or 8080
_G.http_response_error_json = function (res, err_str)
local json_str = json.encode({error = err_str})
-- res:setHeader("Content-Type", "application/json")
-- res:setHeader("Content-Length", #json_str)
res:write(err_str)
-- res:finish(err_str)
end
_G.http_response_ok_json = function (res, data)
local json_str = json.encode({data = data})
-- res:setHeader("Content-Type", "application/json")
-- res:setHeader("Content-Length", #json_str)
res:write(json_str)
-- res:finish(json_str)
end
local function http_is_api_token(str_token)
if str_token == nil then return false end
for i = 1, #tokens do
if str_token == tokens[i] then return true end
end
end
_G.http_backend_register = function (path, cb)
http_emitter:on('/api/'..path, cb)
end
http_backend_register('ping', function (res, http_json)
http_response_ok_json(res, "Have a very safe day.")
end)
function onRequest (req, res)
if not (req.method == 'POST') then http_response_error_json(res, "Only POST requests") end
local chunks = {}
local length = 0
local api_url =
req:on('data', function (chunk)
p({url = req.url, request=chunk})
length = length + 1
chunks[length] = chunk
end)
res:setHeader("Content-Type", "application/json")
req:on('end', function ()
local body = table.concat(chunks, "")
local body_json = json.decode(body)
if body_json == nil then http_response_error_json(res, "Bad json") return end
if not http_is_api_token(body_json.token) then http_response_error_json(res, "Bad token") return end
if http_emitter:listeners(req.url)[1] ~= nil then coroutine.wrap(http_emitter:listeners(req.url)[1](res, body_json)) else http_response_error_json(res, "No handlers for this path") end
res:finish()
end)
end
http.createServer(onRequest):listen(listen_port)
require('modules/exec')
require('modules/accounts_link')
require('modules/privileges')
require('modules/qiwidonate')