-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.lua
More file actions
35 lines (32 loc) · 1.04 KB
/
db.lua
File metadata and controls
35 lines (32 loc) · 1.04 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
local os = require('ffi').os
local env = require('env')
local tmpdir = os == 'Windows' and env.get('TMP') or '/tmp'
local db = require("./luz/db").DB:new("sqlite3", tmpdir.."/test.sqlite3")
local app = require("./luz/app").app:new()
local JSON = require('rapidjson')
-- prepare db
db:run"DROP TABLE user"
db:run[[
CREATE TABLE user(
id INT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(50)
)
]]
local list = {
{ id=1, name="Jose das Couves", email="jose@couves.com", },
{ id=2, name="Manoel Joaquim", email="manoel.joaquim@cafundo.com", },
{ id=3, name="Maria das Dores", email="maria@dores.com", },
}
for i, p in pairs (list) do
res = assert (db:run(string.format([[
INSERT INTO user
VALUES ('%s', '%s', '%s')]], p.id, p.name, p.email)
))
end
app:get('/user/:id', function(params)
local user = db:select("select id, name, email from user where id = "..params.id, true)
return JSON.encode(user)
end)
app:listen({port=8003})
print("Http Server listening at http://0.0.0.0:8003/user/:id")