Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Redbean sample #2

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading