forked from fhirbase/fhirbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
97 lines (78 loc) · 2.18 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"github.com/jackc/pgx"
)
// PgConnectionConfig struct stores credentials for PG connection
type PgConnectionConfig struct {
SSLMode string
Host string
Port uint
Username string
Database string
Password string
}
// PgConnectionConfig holds PG credentials passed from command line
var PgConfig = PgConnectionConfig{}
func GetPgxConnectionConfig(cfg *pgx.ConnConfig) pgx.ConnConfig {
mainConfig := pgx.ConnConfig{
TLSConfig: nil,
Host: PgConfig.Host,
Port: uint16(PgConfig.Port),
User: PgConfig.Username,
Password: PgConfig.Password,
Database: PgConfig.Database,
}
// Match libpq default behavior
if PgConfig.SSLMode == "" {
PgConfig.SSLMode = "prefer"
}
switch PgConfig.SSLMode {
case "disable":
mainConfig.UseFallbackTLS = false
mainConfig.TLSConfig = nil
mainConfig.FallbackTLSConfig = nil
case "allow":
mainConfig.UseFallbackTLS = true
mainConfig.FallbackTLSConfig = &tls.Config{InsecureSkipVerify: true}
case "prefer":
mainConfig.TLSConfig = &tls.Config{InsecureSkipVerify: true}
mainConfig.UseFallbackTLS = true
mainConfig.FallbackTLSConfig = nil
case "require":
mainConfig.TLSConfig = &tls.Config{InsecureSkipVerify: true}
case "verify-ca", "verify-full":
mainConfig.TLSConfig = &tls.Config{
ServerName: mainConfig.Host,
}
default:
panic("--sslmode param is invalid")
}
envConfig, err := pgx.ParseEnvLibpq()
if err == nil {
mainConfig = envConfig.Merge(mainConfig)
}
if cfg != nil {
mainConfig = mainConfig.Merge(*cfg)
}
return mainConfig
}
// GetConnection connects to database
func GetConnection(cfg *pgx.ConnConfig) *pgx.Conn {
mainConfig := GetPgxConnectionConfig(cfg)
connStr := fmt.Sprintf("dbname=%s sslmode=disable user=%s password=%s host=%s port=%d",
mainConfig.Database, mainConfig.User, mainConfig.Password, mainConfig.Host, mainConfig.Port)
conn, err := pgx.Connect(mainConfig)
if err != nil {
log.Fatalf("Error connecting to database: %v", err)
}
err = conn.Ping(context.Background())
if err != nil {
log.Fatalf("Error testing database connection: %v", err)
}
fmt.Printf("Connected to database %s\n", connStr)
return conn
}