-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdb.go
60 lines (50 loc) · 1.46 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
package main
import (
"context"
"time"
// Data
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/log/logrusadapter"
"github.com/jackc/pgx/v4/pgxpool"
// Config
"github.com/spf13/viper"
// Logging
log "github.com/sirupsen/logrus"
)
func dbConnect() (*pgxpool.Pool, error) {
if globalDb == nil {
dbConnection := viper.GetString("DbConnection")
config, err := pgxpool.ParseConfig(dbConnection)
if err != nil {
log.Fatal(err)
}
// Read and parse connection lifetime
dbPoolMaxLifeTime, errt := time.ParseDuration(viper.GetString("DbPoolMaxConnLifeTime"))
if errt != nil {
log.Fatal(errt)
}
config.MaxConnLifetime = dbPoolMaxLifeTime
// Read and parse max connections
dbPoolMaxConns := viper.GetInt32("DbPoolMaxConns")
if dbPoolMaxConns > 0 {
config.MaxConns = dbPoolMaxConns
}
// Read current log level and use one less-fine level
// below that
config.ConnConfig.Logger = logrusadapter.NewLogger(log.New())
levelString, _ := (log.GetLevel() - 1).MarshalText()
pgxLevel, _ := pgx.LogLevelFromString(string(levelString))
config.ConnConfig.LogLevel = pgxLevel
// Connect!
globalDb, err = pgxpool.ConnectConfig(context.Background(), config)
if err != nil {
log.Fatal(err)
}
dbName := config.ConnConfig.Config.Database
dbUser := config.ConnConfig.Config.User
dbHost := config.ConnConfig.Config.Host
log.Infof("Connected as '%s' to '%s' @ '%s'", dbUser, dbName, dbHost)
return globalDb, err
}
return globalDb, nil
}