🎉 initial commit
This commit is contained in:
33
devices-worker/db.lua
Normal file
33
devices-worker/db.lua
Normal file
@@ -0,0 +1,33 @@
|
||||
-- PostgreSQL using pgmoon (worker: one connection per process is fine)
|
||||
local pgmoon = require("pgmoon")
|
||||
|
||||
local DB_HOST = os.getenv("DB_HOST") or "localhost"
|
||||
local DB_PORT = tonumber(os.getenv("DB_PORT")) or 5432
|
||||
local DB_NAME = os.getenv("DB_NAME") or "handheld_devices"
|
||||
local DB_USER = os.getenv("DB_USER") or "devices_user"
|
||||
local DB_PASSWORD = os.getenv("DB_PASSWORD") or "devices_password"
|
||||
local DB_CONNECT_TIMEOUT_MS = tonumber(os.getenv("DB_CONNECT_TIMEOUT_MS")) or 5000
|
||||
|
||||
local config = {
|
||||
host = DB_HOST,
|
||||
port = tostring(DB_PORT),
|
||||
database = DB_NAME,
|
||||
user = DB_USER,
|
||||
password = DB_PASSWORD,
|
||||
socket_type = "luasocket",
|
||||
}
|
||||
|
||||
local function get_connection()
|
||||
local pg = pgmoon.new(config)
|
||||
pg:settimeout(DB_CONNECT_TIMEOUT_MS)
|
||||
local ok, err = pg:connect()
|
||||
if not ok then
|
||||
return nil, err
|
||||
end
|
||||
return pg
|
||||
end
|
||||
|
||||
return {
|
||||
get_connection = get_connection,
|
||||
config = config,
|
||||
}
|
||||
Reference in New Issue
Block a user