/
postgresql.go
72 lines (63 loc) · 1.54 KB
/
postgresql.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
package postgresql
import (
"database/sql"
"fmt"
"net/url"
"github.com/NYTimes/gizmo/config"
)
// Config holds everything you need to
// connect and interact with a PostgreSQL DB.
type Config struct {
User string `envconfig:"POSTGRESQL_USER"`
Pw string `envconfig:"POSTGRESQL_PW"`
Host string `envconfig:"POSTGRESQL_HOST_NAME"`
Port int `envconfig:"POSTGRESQL_PORT"`
DBName string `envconfig:"POSTGRESQL_DB_NAME"`
SSLMode string `envconfig:"POSTGRESQL_SSL_MODE"`
}
const (
// DefaultSSLMode is verify-full
DefaultSSLMode = "verify-full"
// DefaultPort is the default post for Postgresql connections
DefaultPort = 5432
)
// DB will open a sql connection.
// Users must import a postgresql driver in their
// main to use this.
func (p *Config) DB() (*sql.DB, error) {
db, err := sql.Open("postgres", p.String())
if err != nil {
return db, err
}
return db, nil
}
// String will return the Postgresql connection string
func (p *Config) String() string {
if p.Port == 0 {
p.Port = DefaultPort
}
if p.SSLMode != "" {
p.SSLMode = url.QueryEscape(p.SSLMode)
} else {
p.SSLMode = url.QueryEscape(DefaultSSLMode)
}
return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
p.User,
p.Pw,
p.Host,
p.Port,
p.DBName,
p.SSLMode,
)
}
// LoadConfigFromEnv will attempt to load a Postgresql object
// from environment variables. If not populated, nil
// is returned
func LoadConfigFromEnv() *Config {
var postgres Config
config.LoadEnvConfig(&postgres)
if postgres.Host != "" {
return &postgres
}
return nil
}