-
Notifications
You must be signed in to change notification settings - Fork 0
/
myapp.lua
154 lines (136 loc) · 3.73 KB
/
myapp.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
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
#!/usr/bin/env tarantool
local http_router = require('http.router')
local http_server = require('http.server')
local json = require('json')
local log = require('log')
local httpd = http_server.new(
os.getenv("SERVER_IP"),
os.getenv("SERVER_PORT"), {
log_requests = true,
log_errors = true
})
local router = http_router.new()
box.cfg {
listen = os.getenv("LISTEN_URI"),
log_format = 'plain',
log = 'app.log',
background = true,
memtx_memory = 128 * 1024 *1024,
pid_file = 'app.pid'
}
box.once('create', function()
box.schema.space.create('dict')
box.space.dict:format({
{ name = 'key', type = 'string' },
{ name = 'val', type = 'string' }
})
box.space.dict:create_index('primary',
{ type = 'hash', parts = { 1, 'string' } })
end)
function isKeyValCorrect(key, val)
local ok, err
ok, err = pcall(json.decode, val)
if key == nil or type(key) ~= 'string' then
log.info("Error: key invalid")
log.info("Key:")
log.info(key)
return false
elseif not ok or type(val) ~= 'string' then
log.info("Error: val invalid")
log.info("Val:")
log.info(val)
log.info(err)
return false
end
return true
end
function isJsonPostCorrect(req)
local ok, kv, key, val
ok, kv = pcall(req.json, req)
if ok then
key, val = kv['key'], kv['value']
if not isKeyValCorrect(key, val) then
return false
end
else
log.info("Error: body json invalid")
log.info(kv)
return false
end
return true, key, val
end
function isJsonPutCorrect(req)
local ok, kv, key, val
ok, kv = pcall(req.json, req)
if ok then
key, val = req:stash('key'), kv['value']
if not isKeyValCorrect(key, val) then
return false
end
else
log.info("Error: body json invalid")
return false
end
return true, key, val
end
router:route({ method = 'POST', path = '/kv' }, function(req)
local ok, key, val = isJsonPostCorrect(req)
if not ok then
return { status = 400 }
end
kv = box.space.dict:get(key)
if kv == nil or #kv == 0 then
box.space.dict:insert { key, val }
log.info("Ok, key, val:")
log.info(key)
log.info(val)
return { status = 200 }
else
log.info("Error: Key already exist:")
log.info(key)
return { status = 409 }
end
end)
router:route({ method = 'PUT', path = '/kv/:key' }, function(req)
local ok, key, val = isJsonPutCorrect(req)
if not ok then
return { status = 400 }
end
kv = box.space.dict:get(key)
if kv == nil or #kv == 0 then
log.info("Error: Key not found")
return { status = 404 }
else
box.space.dict:update(key, { { '=', 2, val } })
log.info("Ok, new value:")
log.info(val)
return { status = 200 }
end
end)
router:route({ method = 'GET', path = '/kv/:key' }, function(req)
local key, val, kv
key = req:stash('key')
kv = box.space.dict:get(key)
if kv == nil or #kv == 0 then
log.info("Error: Key not found, key: " .. key)
return { status = 404 }
end
val = kv[2]
log.info("Ok, value:")
log.info(val)
return { status = 200, body = val }
end)
router:route({ method = 'DELETE', path = '/kv/:key' }, function(req)
local key, kv
key = req:stash('key')
kv = box.space.dict:get(key)
if kv == nil or #kv == 0 then
log.info("Error: Key not found, key: " .. key)
return { status = 404 }
end
box.space.dict:delete(key)
log.info("Ok, key: " .. key)
return { status = 200 }
end)
httpd:set_router(router)
httpd:start()