Skip to content

Commit

Permalink
Merge pull request #2 from secjoa/patch-3
Browse files Browse the repository at this point in the history
Update Redbean sample
  • Loading branch information
HereAdvertise authored Jul 26, 2024
2 parents 0f3bdef + f0367bb commit 8c67fe3
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 8c67fe3

Please sign in to comment.