-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.lua
105 lines (93 loc) · 2.7 KB
/
handler.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
local log = require('log')
local json = require('json')
local handler = {}
handler.path_post = '/kv'
handler.post_method = function(req)
local status, body = pcall(req.json, req)
local key, val = body['key'], body['value']
if (status == false) or (type(key) ~= 'string') or
(type(val) ~= 'table') then
log.info('ER: invalid data')
return { status = 400 }
end
local status, data = pcall(
box.space.dict.insert, box.space.dict, {key, val})
if status then
log.info('OK')
log.info(data)
return { status = 200 }
else
log.info('ER: ' .. data.message)
if data.code == 3 then -- ER_TUPLE_FOUND
return { status = 409 }
else
return { status = 500 }
end
end
end
handler.path_put = '/kv/:key'
handler.put_method = function(req)
local key = req:stash('key')
log.info(key)
local status, body = pcall(req.json, req)
local val = body['value']
if (status == false) or (type(val) ~= 'table') then
log.info('ER: invalid data')
return { status = 400 }
end
local status, data = pcall(
box.space.dict.update, box.space.dict, key, { {'=', 2, val} })
if data == nil then
log.info('ER: key not found: ' .. key )
return { status = 404 }
elseif status then
log.info('OK')
log.info(data)
return { status = 200 }
else
log.info('ER: ' .. data.message)
end
end
handler.path_get = '/kv/:key'
handler.get_method = function(req)
local key = req:stash('key')
local status, data = pcall(
box.space.dict.get, box.space.dict, key)
if status and data then
log.info('OK')
log.info(data)
return {
status = 200,
body = json.encode(data[2])
}
elseif data == nil then
log.info('ER: key not found: ' .. key )
return { status = 404 }
else
log.info('ER: ' .. data.message)
return { status = 500 } -- может быть ER_NO_SUCH_SPACE
end
end
handler.path_delete = '/kv/:key'
handler.delete_method = function(req)
local key = req:stash('key')
local status, data = pcall(
box.space.dict.delete, box.space.dict, key)
log.info(data)
log.info(status)
if status and data then
log.info('OK')
log.info(data)
return {
status = 200,
body = json.encode(data[2])
}
elseif data == nil then
log.info('ER: key not found: ' .. key )
return { status = 404 }
else
log.info('ER: ' .. data.message)
return { status = 500 } -- может быть ER_NO_SUCH_SPACE
end
end
return handler