-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
56 lines (44 loc) · 1.4 KB
/
db.go
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
50
51
52
53
54
55
56
package goa
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v4/pgxpool"
)
// newDBClient ...
func (config *Config) newDBClient() *pgxpool.Pool {
// NOTE: Configure database
// https://pkg.go.dev/github.com/jackc/pgx/v4@v4.11.0/pgxpool#ParseConfig
// Example URL
// "postgres://username:password@localhost:5432/database_name"
// Example DSN
// "user=jack password=secret host=pg.example.com port=5432 dbname=mydb sslmode=verify-ca pool_max_conns=10"
var connString string
if os.Getenv("DATABASE_URL") != "" {
connString = os.Getenv("DATABASE_URL")
} else {
connString = fmt.Sprintf("user=%v password=%v host=%v port=%v dbname=%v sslmode=disable pool_max_conns=5",
config.Database.Username,
config.Database.Password,
config.Database.Host,
config.Database.Port,
config.Database.Database,
)
}
// NOTE: Using github.com/jackc/pgx/v4
// https://pkg.go.dev/github.com/jackc/pgx/v4@v4.11.0/pgxpool#pkg-overview
dbConfig, err := pgxpool.ParseConfig(connString)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to parse database config: %v\n", err)
os.Exit(1)
}
// dbConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
// // do something with every new connection
// }
dbpool, err := pgxpool.ConnectConfig(context.Background(), dbConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
return dbpool
}