From f0367bb94d7969ea2492b5e2982a5f19bbc39303 Mon Sep 17 00:00:00 2001 From: secjoa <160734411+secjoa@users.noreply.github.com> Date: Thu, 25 Jul 2024 14:09:43 -0300 Subject: [PATCH] Update Redbean sample --- README.md | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0220c97..19fd766 100644 --- a/README.md +++ b/README.md @@ -121,24 +121,31 @@ curl https://redbean.dev/redbean-latest.com >redbean.com chmod +x redbean.com ``` -Create the file `test.lua` to access the Postgres database. +Create the file `.init.lua` to access the Postgres database. ```lua --- test.lua - +-- .init.lua local pgmoon = require("pgmoon") -local pg = pgmoon.new({ - host = "127.0.0.1", - port = "5432", - database = "database_name", - user = "database_user", - password = "database_password" -}) +function OnWorkerStart() + _G.pg = pgmoon.new({ + host = "127.0.0.1", + port = "5432", + database = "mydb", + user = "postgres" + }) + assert(pg:connect()) +end -assert(pg:connect()) +function OnWorkerStop() + assert(pg:disconnect()) +end + +function OnHttpRequest() + local res = assert(pg:query("SELECT 'hello world';")) -print(EncodeJson(assert(pg:query("SELECT 'hello world';")))) + Write(EncodeJson(res, {pretty = true})) +end ``` The Redbean server operates within a zipped structure. To integrate pgmoon into this structure, follow the steps below: @@ -149,10 +156,10 @@ mv pgmoon .lua zip -r redbean.com .lua ``` -Now just execute the `test.lua` file. +Now just execute the Redbean server. ``` -./redbean.com -i test.lua +./redbean.com ``` ## Considerations