-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.lua
113 lines (87 loc) · 2.88 KB
/
server.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
local app = require "milua"
local json = require "dkjson"
local logger = require("milua_log")
local data = {}
local function database_location()
local location = os.getenv("DATA_LOCATION")
if location == nil then
location = "db.json"
else
local ending = ".json"
if location:sub(-#ending) ~= ending then
location = location .. "/db.json"
end
local directory = location:match("(.*/)")
if directory ~= nil then os.execute("mkdir -p " .. directory) end
end
return location
end
local db_location = database_location()
print("Database location: " .. db_location)
-- function that loads data from a file
local function load_data()
local file = io.open(db_location, "r")
if file then
data = json.decode(file:read("*a")) or {}
file:close()
end
end
-- function that saves data to a file
local function save_data()
local file = io.open(db_location, "w")
if file then
file:write(json.encode(data))
file:close()
end
end
local function check_input(array)
if array == nil or type(array) ~= "table" or #array == 0 then
return false
end
for _, v in pairs(array) do if type(v) ~= "string" then return false end end
return true
end
if os.getenv("TESTING") == "true" then
app.add_handler("DELETE", "/inspirations/", function()
data = {}
save_data()
return nil, {[":status"] = "204"}
end)
end
app.add_handler("GET", "/health",
function() return nil, {[":status"] = "204"} end)
app.add_handler("GET", "/inspirations/...", function(captures)
local recipe_id = captures[1]
local inspirations = data[recipe_id]
if inspirations == nil then return nil, {[":status"] = "404"} end
return json.encode(inspirations), {["content-type"] = "application/json"}
end)
app.add_handler("PUT", "/inspirations/...", function(captures, _q, _h, body)
local recipe_id = captures[1]
local decoded = json.decode(body)
if not check_input(decoded) then
return "Input must be an json array of strings.",
{["Content-Type"] = "text/plain", [":status"] = "400"}
end
data[recipe_id] = {}
for _, v in pairs(decoded) do table.insert(data[recipe_id], v) end
save_data()
return nil, {[":status"] = "201"}
end)
app.add_handler("DELETE", "/inspirations/...", function(captures)
local recipe_id = captures[1]
local inspirations = data[recipe_id]
if inspirations == nil then return nil, {[":status"] = "404"} end
data[recipe_id] = nil
save_data()
return nil, {[":status"] = "204"}
end)
load_data()
if os.getenv("VERBOSE") ~= "true" then
local function noop() end
logger:add_logger("INFO", noop)
logger:add_logger("DEBUG", noop)
end
local config = {HOST = "0.0.0.0", PORT = 8800}
print("Starting server on " .. config.HOST .. ":" .. config.PORT .. "...")
app.start(config)