DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: 6124b16e89cc33cf9e3b2df29c709b1f1465ac53 tixe/db/db.go -rw-r--r-- 1.1 KiB
6124b16eJonni Liljamo fix: abort if not logged in... 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */
package db

import (
	"context"
	"fmt"
	"log"
	"time"
	"tixe/config"

	"github.com/jackc/pgx/v5/pgxpool"
)

var PgPool *pgxpool.Pool

func NewPgPool() {
	var err error
	PgPool, err = pgxpool.New(context.Background(), pgConnectionString())
	if err != nil {
		log.Fatalf("[tixe/db] Unable to create psql connection pool: '%s'", err)
	}
	log.Print("[tixe/db] Created psql pool")

	for {
		err = PgPool.Ping(context.Background())
		if err != nil {
			log.Print("[tixe/db] Database not ready, retrying in 5")
			time.Sleep(5 * time.Second)
		} else {
			log.Print("[tixe/db] Database connection verified")
			return
		}
	}
}

func pgConnectionString() string {
	user := config.TixeConfig.PsqlUser
	pwd := config.TixeConfig.PsqlPwd
	host := config.TixeConfig.PsqlHost
	port := config.TixeConfig.PsqlPort
	db := config.TixeConfig.PsqlDb

	return fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=disable", user, pwd, host, port, db)
}