Repository navigation
Expand file tree
/
Copy pathtodo_server.lua
More file actions
executable file
·144 lines (122 loc) · 3.67 KB
/
Copy pathtodo_server.lua
File metadata and controls
executable file
·144 lines (122 loc) · 3.67 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env lua
--[[
A simple todo-list server example.
This example requires the restserver-xavante rock to run.
A fun example session:
curl localhost:8080/todo
curl -v -H "Content-Type: application/json" -X POST -d '{ "task": "Clean bedroom" }' http://localhost:8080/todo
curl -v localhost:8080/todo
curl -v -H "Content-Type: application/json" -X POST -d '{ "task": "Groceries" }' http://localhost:8080/todo
curl -v localhost:8080/todo
curl -v localhost:8080/todo/2/status
curl -v localhost:8080/todo/2/done
curl -v localhost:8080/todo/2/status
curl -v localhost:8080/todo/9/status
curl -v -H "Content-Type: application/json" -X DELETE http://localhost:8080/todo/2
curl -v localhost:8080/todo
]]
local restserver = require("restserver")
local server = restserver:new():port(8080)
local todo_list = {}
local next_id = 0
server:add_resource("todo", {
{
method = "GET",
path = "/",
produces = "application/json",
handler = function()
return restserver.response():status(200):entity(todo_list)
end,
},
{
method = "POST",
path = "/",
consumes = "application/json",
produces = "application/json",
input_schema = {
task = { type = "string" },
},
handler = function(task_submission)
print("Received task: " .. task_submission.task)
next_id = next_id + 1
task_submission.id = next_id
task_submission.done = false
table.insert(todo_list, task_submission)
local result = {
id = task_submission.id
}
return restserver.response():status(200):entity(result)
end,
},
{
method = "GET",
path = "{id:[0-9]+}/status",
produces = "application/json",
handler = function(_, id)
for _, task in ipairs(todo_list) do
if task.id == tonumber(id) then
return restserver.response():status(200):entity(task)
end
end
return restserver.response():status(404):entity("Id not found.")
end,
},
{
method = "GET",
path = "{id:[0-9]+}/done",
produces = "application/json",
handler = function(_, id)
for _, task in ipairs(todo_list) do
if task.id == tonumber(id) then
task.done = true
return restserver.response():status(200):entity(task)
end
end
return restserver.response():status(404):entity("Id not found.")
end,
},
{
method = "DELETE",
path = "{id:[0-9]+}",
produces = "application/json",
handler = function(_, id)
for i, task in ipairs(todo_list) do
if task.id == tonumber(id) then
table.remove(todo_list, i)
return restserver.response():status(200):entity(task)
end
end
return restserver.response():status(404):entity("Id not found.")
end,
},
{
method = "GET",
path = "/reset",
produces = "application/json",
handler = function()
todo_list = {}
next_id = 0
return restserver.response():status(200)
end,
},
})
-- You can create your custom error handler
server:set_error_handler({
produces = "application/json",
handler = function(code, msg)
return {
code = code,
message = msg,
custom_error_field = "Think about it!",
}
end,
})
-- You can report your own server name
server.server_name = "My To-do Server"
--print("Listening for:")
--local path_item
--for _,path_item in ipairs(server.config.path_list) do
-- print(path_item)
--end
-- This loads the restserver.xavante plugin
server:enable("restserver.xavante"):start()