-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexample-requests.lua
52 lines (45 loc) · 1.45 KB
/
example-requests.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
nextId = 0
HttpRequests["/notes"] = {
GET = function(request)
response = {}
response.status = 200
response.content = {
}
if request.id ~= nil then
if file.open(request.id .. ".note") ~= nil then
record = json.decode(file.read())
file.close()
response.content.text = record.content
else
response.status = 404
response.content = nil
end
else
response.content.notes = {}
local fileListing = file.list()
for name, size in pairs(fileListing) do
if string.sub(name,-5)==".note" then
response.content.notes[string.sub(name, 1, string.len(name) - 5)] = { size = size }
end
end
end
return response
end,
POST = function(request)
print(request.content.text)
print(node.heap())
collectgarbage()
collectgarbage()
print(node.heap())
response = {}
print(request.content.text)
local fileContent = { content = request.content.text }
file.open(nextId .. ".note", "w")
file.write(json.encode(fileContent))
file.close()
response.headers = { Location = "/notes/" .. nextId }
response.status = 201
nextId = nextId + 1
return response
end,
}