-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
59 lines (51 loc) · 1.27 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
package main
import (
"os"
"time"
"strconv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var DB *gorm.DB
func SetupDB() {
db_conn_string := os.Getenv("DB_CONNECTION_STRING")
if db_conn_string == "" {
panic("DB_CONNECTION_STRING not specified, exiting :(\n")
return
}
var err error
DB, err = gorm.Open(postgres.Open(db_conn_string))
if err != nil {
panic(err)
}
sqlDB, err := DB.DB()
if err != nil {
panic(err)
}
err = sqlDB.Ping()
if err != nil {
panic(err)
}
max_idle_conns := os.Getenv("DB_MAX_IDLE_CONNECTIONS")
if max_idle_conns == "" {
panic("DB_MAX_IDLE_CONNECTIONS not set")
}
max_open_conns := os.Getenv("DB_MAX_OPEN_CONNECTIONS")
if max_open_conns == "" {
panic("DB_MAX_OPEN_CONNECTIONS not set")
}
max_idle_conns_i, err := strconv.Atoi(max_idle_conns)
if err != nil {
panic(err)
}
max_open_conns_i, err := strconv.Atoi(max_open_conns)
if err != nil {
panic(err)
}
// Maximum Idle Connections
sqlDB.SetMaxIdleConns(max_idle_conns_i)
// Maximum Open Connections
sqlDB.SetMaxOpenConns(max_open_conns_i)
// Connection Lifetime
sqlDB.SetConnMaxLifetime(30 * time.Second)
}