Files
Arjun Patel 563c91e7d5 infra: add logging wrapping
Resolves issues with gcp cloud logging quirks such as field names
2026-05-27 15:49:27 -07:00

46 lines
837 B
Go

package db
import (
"context"
"os"
"github.com/flowy-live/llink/internal/utils/flog"
"github.com/jackc/pgx/v5/pgxpool"
)
var db *pgxpool.Pool
func Cleanup() {
db.Close()
}
func Pool() *pgxpool.Pool {
return db
}
func Init() {
connString := os.Getenv("LLINK_POSTGRES_CONNECTION_URL")
if connString == "" {
flog.Error("must provide LLINK_POSTGRES_CONNECTION_URL in env")
os.Exit(1)
}
dbpool, err := pgxpool.New(context.Background(), connString)
if err != nil {
flog.Error("unable to create connection pool", "error", err)
os.Exit(1)
}
var greeting string
err = dbpool.QueryRow(context.Background(), "select 'Hello, world!'").Scan(&greeting)
if err != nil {
flog.Error("queryRow failed", "error", err)
os.Exit(1)
}
flog.Info("successfully connected to database", "greeting", greeting)
db = dbpool
}