This commit is contained in:
Atridad Lahiji 2024-10-16 21:14:19 -06:00
parent 424bbae5c2
commit 8e4edf31c7
Signed by: atridad
SSH key fingerprint: SHA256:LGomp8Opq0jz+7kbwNcdfTcuaLRb5Nh0k5AchDDb438
2 changed files with 17 additions and 6 deletions

View file

@ -27,9 +27,10 @@ func GenerateNewID(prefix string) string {
} }
// Initializes the global database connection pool. // Initializes the global database connection pool.
func InitializeDBPool(databaseURL string) error { func InitializeDBPool(host, user, password, dbname string, port int) error {
connString := fmt.Sprintf("postgres://%s:%s@%s:%d/%s", user, password, host, port, dbname)
var err error var err error
dbPool, err = pgxpool.Connect(context.Background(), databaseURL) dbPool, err = pgxpool.Connect(context.Background(), connString)
if err != nil { if err != nil {
return err return err
} }

18
main.go
View file

@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"strconv"
"pollo/api" "pollo/api"
"pollo/api/webhooks" "pollo/api/webhooks"
@ -26,12 +27,21 @@ func main() {
godotenv.Load(".env") godotenv.Load(".env")
// Initialize the database connection pool // Initialize the database connection pool
databaseURL := os.Getenv("DATABASE_URL") postgresHost := os.Getenv("POSTGRES_HOST")
if databaseURL == "" { postgresPort := os.Getenv("POSTGRES_PORT")
log.Fatal("DATABASE_URL environment variable not set") postgresUser := os.Getenv("POSTGRES_USER")
postgresPassword := os.Getenv("POSTGRES_PASSWORD")
postgresDB := os.Getenv("POSTGRES_DB")
if postgresHost == "" || postgresPort == "" || postgresUser == "" || postgresPassword == "" || postgresDB == "" {
log.Fatal("DB environment variables not set")
} }
if err := lib.InitializeDBPool(databaseURL); err != nil { portNumber, err := strconv.Atoi(postgresPort)
if err != nil {
log.Fatalf("Invalid database port: %v", err)
}
if err := lib.InitializeDBPool(postgresHost, postgresUser, postgresPassword, postgresDB, portNumber); err != nil {
log.Fatalf("Failed to initialize DB pool: %v", err) log.Fatalf("Failed to initialize DB pool: %v", err)
} }