-
Notifications
You must be signed in to change notification settings - Fork 0
/
zenswarm-router.lua
79 lines (73 loc) · 2.44 KB
/
zenswarm-router.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
local cartridge = require('cartridge')
local crud = require('crud')
local log = require('log')
local function init(opts) -- luacheck: no unused args
if opts.is_master then
local httpd = assert(cartridge.service_get('httpd'), 'Failed to get httpd service')
httpd:route({
method = 'GET',
path = '/values',
public = true,
},
function(req)
local result, err = crud.select('values', nil, { first = 10000 })
if err then
return { status = 500, body = err }
end
return req:render({ json = crud.unflatten_rows(result.rows, result.metadata) })
end
)
httpd:route({
method = 'GET',
path = '/retrieve/:key',
},
function(req)
if not req.tstash.key then
return { status = 400, body = 'Missing key' }
end
local result, err = crud.get('values', req.tstash.key)
if err then
return { status = 500, body = err }
end
return req:render({ json = crud.unflatten_rows(result.rows, result.metadata) })
end
)
httpd:route({
method = 'POST',
path = '/retrieve',
},
function(req)
local body = req:json()
log.error(body)
if not body.key then
return { status = 400, body = 'Missing key' }
end
local result, err = crud.get('values', body.key)
if err then
return { status = 500, body = err }
end
return req:render({ json = crud.unflatten_rows(result.rows, result.metadata) })
end
)
httpd:route({
method = 'POST',
path = '/store',
public = true,
},
function(req)
local body = req:json()
log.info('body: ', body)
local result, err = crud.insert_object('values', body)
if err then
return { status = 500, body = err }
end
return { status = 200, body = result }
end
)
end
end
return {
role_name = 'app.roles.zenswarm-router',
init = init,
dependencies = { 'cartridge.roles.crud-router' },
}