Skip to content

Commit 8e4e272

Browse files
author
Olivier Bonnaure
committed
fix: Fix PGRest & Redis
1 parent 6fedad9 commit 8e4e272

File tree

6 files changed

+62
-27
lines changed

6 files changed

+62
-27
lines changed

.init.lua

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ I18n = I18nClass.new("en")
1515
ProgramMaxPayloadSize(10485760) -- 10 MB
1616

1717
-- DB connection
18-
-- local db_config = DecodeJson(LoadAsset("config/database.json"))
19-
-- InitDB(db_config)
18+
local db_config = DecodeJson(LoadAsset("config/database.json"))
19+
InitDB(db_config)
2020

2121
Views = {}
2222
isApi = false
@@ -60,15 +60,19 @@ end
6060

6161
-- OnHttpRequest hook
6262
function OnHttpRequest()
63+
64+
-- local redis = require "db.redis"
65+
-- Redis = redis.connect()
66+
6367
Params = GetParams()
6468
PrepareMultiPartParams() -- if you handle file uploads
6569

6670
SetDevice() -- comment if you do not need this feature
6771

6872
-- Uncomment code if you use ArangoDB
69-
-- if Adb then
70-
-- Adb.primary:RefreshToken() -- reconnect to arangoDB if needed
71-
--end
73+
if Adb then
74+
Adb.primary:RefreshToken() -- reconnect to arangoDB if needed
75+
end
7276

7377
-- Uncomment code if you use surrealdb
7478
-- if (db_config ~= nil and db_config["engine"] == "surrealdb") then
@@ -77,7 +81,10 @@ function OnHttpRequest()
7781

7882
DefineRoute(GetPath(), GetMethod())
7983

84+
8085
HandleRequest()
86+
-- Uncomment if you use redis
87+
-- unix.close(Redis.network.socket)
8188
end
8289

8390
function SetDevice()
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,47 @@ function PGRest.new(db_config)
88
self._headers = {}
99
self:init()
1010

11-
return PGRest
11+
return self
1212
end
1313

1414
function PGRest:init()
15-
PGRestApiUrl = db_config["url"]
15+
self._api_url = self._db_config["url"]
1616
self._headers = {
17-
["Authorization"] = "Bearer " .. db_config["jwt_token"],
17+
["Authorization"] = "Bearer " .. self._db_config["jwt_token"],
1818
["Content-Type"] = "application/json"
1919
}
2020
end
2121

2222
function PGRest:count(path)
23-
local ok, h, body = Fetch(PGRestApiUrl .. path, { headers = table.merge(self._headers, { Prefer = "count=exact" }) })
23+
local ok, h, body = Fetch(self._api_url .. path, { headers = table.merge(self._headers, { Prefer = "count=exact" }) })
2424
return ok, tonumber(string.split(h["Content-Range"], "/")[2])
2525
end
2626

2727
function PGRest:read(path)
28-
local ok, h, body = Fetch(PGRestApiUrl .. path, { headers = self._headers })
28+
local ok, h, body = Fetch(self._api_url .. path, { headers = self._headers })
2929
return ok, DecodeJson(body)
3030
end
3131

3232
function PGRest:update(path, data)
33-
local ok, h, body = Fetch(PGRestApiUrl .. path, {
33+
local ok, h, body = Fetch(self._api_url .. path, {
3434
method = "PATCH",
35-
body = EncodeJson(data),
35+
body = data and EncodeJson(data) or {},
3636
headers = self._headers
3737
})
3838
return ok, DecodeJson(body)
3939
end
4040

4141
function PGRest:insert(path, data)
42-
local ok, h, body = Fetch(PGRestApiUrl .. path, {
42+
local ok, h, body = Fetch(self._api_url .. path, {
4343
method = "POST",
44-
body = EncodeJson(data),
44+
body = data and EncodeJson(data) or {},
4545
headers = table.merge(self._headers, { Prefer = "return=representation" })
4646
})
4747
return ok, body
4848
end
4949

5050
function PGRest:delete(path, data)
51-
local ok, h, body = Fetch(PGRestApiUrl .. path, {
51+
local ok, h, body = Fetch(self._api_url .. path, {
5252
method = "DELETE",
5353
headers = self._headers
5454
})

.lua/db/redis.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,10 @@ end
313313

314314
function response.read(client)
315315
local payload = client.network.read(client)
316-
local prefix, data = payload:sub(1, - #payload), payload:sub(2)
316+
local prefix, data = payload:sub(1, - #payload), payload:sub(2)
317+
-- print("Prefix (%s) Data (%s)" % { prefix, data })
317318
-- status reply
318-
if prefix == '+' then
319+
if prefix == '+' then
319320
if data == 'OK' then
320321
return true
321322
elseif data == 'QUEUED' then
@@ -344,15 +345,15 @@ function response.read(client)
344345
-- bulk reply
345346
elseif prefix == '$' then
346347
local length = tonumber(data:match("^[^\n]*"))
347-
if not length then
348+
if not length then
348349
client.error('cannot parse ' .. length .. ' as data length')
349350
end
350351

351352
if length == -1 then
352353
return nil
353354
end
354355

355-
local nextchunk = data:gsub("^[^\n]*\n", "") .. client.network.read(client, length + 2)
356+
local nextchunk = data:gsub("^[^\n]*\n", "")-- .. client.network.read(client, length + 2)
356357

357358
return nextchunk:sub(1, -3)
358359

@@ -905,6 +906,7 @@ function redis.undefine_command(name)
905906
undefine_command_impl(redis.commands, name)
906907
end
907908

909+
908910
-- ############################################################################
909911

910912
-- Commands defined in this table do not take the precedence over

app/controllers/welcome_controller.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ local app = {
55
-- WriteJSON({ demo = true })
66
end,
77

8+
redis_incr = function()
9+
WriteJSON(Redis:info())
10+
end,
11+
812
pdf = function()
913
PDFGenerator = require("pdfgenerator")
1014

config/routes.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Routes = { ["GET"] = {
22
[""] = "welcome#index",
33
["pdf"] = "welcome#pdf",
4-
["chart"] = "welcome#chart"
4+
["chart"] = "welcome#chart",
5+
["redis-incr"] = "welcome#redis_incr"
56
}
67
}
78

@@ -72,4 +73,4 @@ CustomRoute("GET", "/xy/:uuid/:width/:height/:format", "uploads#resized_image_x_
7273
-- })
7374
-- -- Will generate :
7475
-- -- GET /customers/:customer_id/books -- books#index
75-
-- -- GET /customers/:customer_id/books/:book_id -- books#show
76+
-- -- GET /customers/:customer_id/books/:book_id -- books#show

docker-compose.yml

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
services:
2-
app:
3-
build: .
4-
ports:
5-
- 7000:8080
6-
environment:
7-
BEANS_ENV: production
2+
#app:
3+
# build: .
4+
# ports:
5+
# - 7000:8080
6+
# environment:
7+
# BEANS_ENV: production
88

99
arangodb:
1010
image: arangodb/arangodb:latest
@@ -16,6 +16,27 @@ services:
1616
- arangodb-data:/var/lib/arangodb3
1717
- ./foxx:/var/lib/arangodb3-apps
1818

19+
pgrest:
20+
image: postgrest/postgrest
21+
ports:
22+
- "3000:3000"
23+
environment:
24+
PGRST_DB_URI: postgres://postgres:password@postgres:5432/app_db
25+
PGRST_OPENAPI_SERVER_PROXY_URI: http://127.0.0.1:3000
26+
PGRST_JWT_SECRET: "3ed06358-2894-4c70-8f9c-0d202708f3597ec9272e-0b40-4ea4-9b5b-d6084103b8ae"
27+
PGRST_DB_SCHEMAS: public
28+
depends_on:
29+
- postgres
30+
31+
postgres:
32+
image: postgres
33+
ports:
34+
- "5432:5432"
35+
environment:
36+
POSTGRES_DB: app_db
37+
POSTGRES_USER: postgres
38+
POSTGRES_PASSWORD: password
39+
1940
#db2rest:
2041
# env_file:
2142
# - .env

0 commit comments

Comments
 (0)