-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
183 lines (140 loc) · 4.3 KB
/
server.lua
File metadata and controls
183 lines (140 loc) · 4.3 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env tarantool
local http_router = require('http.router')
local http_server = require('http.server')
local json = require('json')
local log = require('log')
function get_id_from_path(path)
local i = -1
local res = ""
while (path:sub(i, i) ~= '/') do
res = path:sub(i, i)..res
i = i - 1
end
return res
end
assert(arg[1], "Require host")
assert(arg[2], "Require port")
box.cfg{
log = 'server.log',
pid_file = 'server.pid',
log_level = 5,
background = true
}
mainfold = box.schema.create_space('mainfold', {
format = {
{name = 'id'; type = 'string'},
{name = 'data'; type = '*'},
};
if_not_exists = true;
})
box.space.mainfold:create_index('primary', {
parts = {1, 'string'};
if_not_exists = true;
})
local httpd = http_server.new(arg[1], tonumber(arg[2]), {
log_requests = true,
log_errors = true
})
local function get(req)
local id = get_id_from_path(req:path())
local data = box.space.mainfold:get(id)
if (data == nil) then
local message = "GET: id \'%s\' doesn't exist"
log.warn(message:format(id))
return {status = 404, body = json.encode("id doesn't exist")}
end
local message = "GET: tuple with id \'%s\' has been sent"
log.info(message:format(id))
return {status = 200, body = json.encode(data)}
end
local function post(req)
local body = {}
if not pcall(function() body = json.decode(req:read()) end) then
local message = "POST: Invalid body."
log.warn(message)
return {status = 400, body = "Invalid body"}
end
local key = body['key']
local value = body['value']
if (key == nil) or (value == nil) or (type(key) ~= 'string') then
local message = "POST: Invalid body. Key: \'%s\'"
log.warn(message:format(key))
return {status = 400, body = "Invalid body"}
end
local data = box.space.mainfold:get(key)
if(data ~= nil) then
local message = "POST: Key \'%s\' exists"
log.warn(message:format(key))
return {status = 409, body = "Key exists"}
end
data = {key, value}
box.space.mainfold:insert{key, value}
local message = "POST: tuple with key \'%s\' has been posted"
log.info(message:format(key))
return {status = 200, body = json.encode(data)}
end
local function put(req)
local body = {}
if not pcall(function() body = json.decode(req:read()) end) then
local message = "PUT: Invalid body"
log.warn(message)
return {status = 400, body = "Invalid body"}
end
local id = get_id_from_path(req:path())
local value = body['value']
if (value == nil) then
local message = "PUT: Invalid body. id: \'%s\'"
log.warn(message:format(id))
return {status = 400, body = "Invalid body"}
end
local data = box.space.mainfold:get(id)
if(data == nil) then
local message = "PUT: id \'%s\' doesn't exist"
log.warn(message:format(id))
return {status = 404, body = "id doesn't exist"}
end
data = {id, value}
box.space.mainfold:replace{id, value}
local message = "PUT: tuple with id \'%s\' has been updated"
log.info(message:format(id))
return {status = 200, body = json.encode(data)}
end
local function delete(req)
local id = get_id_from_path(req:path())
local data = box.space.mainfold:delete(id)
if (data == nil) then
local message = "DELETE: id \'%s\' doesn't exist"
log.warn(message:format(id))
return {status = 404, body = "id doesn't exist"}
end
local message = "DELETE: tuple with id \'%s\' has been deleted"
log.info(message:format(id))
return {status = 200, body = json.encode("Success")}
end
local router = http_router.new()
:route({
method = 'GET',
path = '/kv/.*',
},
get
)
:route({
method = 'DELETE',
path = '/kv/.*',
},
delete
)
:route({
method = 'POST',
path = '/kv',
},
post
)
:route({
method = 'PUT',
path = '/kv/.*',
},
put
)
httpd:set_router(router)
httpd:start()