-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection.go
141 lines (126 loc) · 3.17 KB
/
connection.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package papergres
import (
"fmt"
"net"
"net/url"
"sort"
"strings"
)
// SSLMode defines all possible SSL options
type SSLMode string
const (
// SSLDisable - No SSL
SSLDisable SSLMode = "disable"
// SSLRequire - Always SSL, no verification
SSLRequire SSLMode = "require"
// SSLVerifyCA - Always SSL, verifies that certificate was signed by trusted CA
SSLVerifyCA SSLMode = "verify-ca"
// SSLVerifyFull - Always SSL, verifies that certificate was signed by trusted CA
// and server host name matches the one in the certificate
SSLVerifyFull SSLMode = "verify-full"
)
// Connection holds all database connection configuration.
type Connection struct {
Database string
User string
Password string
Host string
Port string
AppName string
Timeout int
SSLMode SSLMode
SSLCert string
SSLKey string
SSLRootCert string
}
// NewConnection creates and returns the Connection object to the postgres server
func NewConnection(databaseURL string, appName string) Connection {
u, err := url.Parse(databaseURL)
if err != nil {
panic(err)
}
host, port, _ := net.SplitHostPort(u.Host)
p, _ := u.User.Password()
q, _ := url.ParseQuery(u.RawQuery)
path := u.Path
if strings.Index(path, "/") == 0 {
path = path[1:]
}
// Set sslmode
sslMode := setSSLMode(q)
// Build the Connection object
conn := Connection{
User: u.User.Username(),
Password: p,
Host: host,
Port: port,
Database: path,
AppName: appName,
SSLMode: sslMode,
}
return conn
}
// String method builds a DSN(Data Source Name) connection string based on the
// given database connection settings and returns it.
func (conn *Connection) String() string {
var s string
if conn.Database != "" {
s += fmt.Sprintf("dbname=%s ", conn.Database)
}
if conn.User != "" {
s += fmt.Sprintf("user=%s ", conn.User)
}
if conn.Password != "" {
s += fmt.Sprintf("password=%s ", conn.Password)
}
if conn.Host != "" {
s += fmt.Sprintf("host=%s ", conn.Host)
}
if conn.Port != "" {
s += fmt.Sprintf("port=%s ", conn.Port)
}
if conn.AppName != "" {
s += fmt.Sprintf("fallback_application_name=%s ", conn.AppName)
}
if conn.SSLMode != "" {
s += fmt.Sprintf("sslmode=%s ", conn.SSLMode)
}
if conn.SSLCert != "" {
s += fmt.Sprintf("sslcert=%s ", conn.SSLCert)
}
if conn.SSLKey != "" {
s += fmt.Sprintf("sslkey=%s ", conn.SSLKey)
}
if conn.SSLRootCert != "" {
s += fmt.Sprintf("sslrootcert=%s ", conn.SSLRootCert)
}
s += fmt.Sprintf("connect_timeout=%v ", conn.Timeout)
return s
}
// NewDatabase creates a new Database object
func (conn Connection) NewDatabase() *Database {
return &Database{
conn: &conn,
}
}
// function to set SSL mode for connection based on url.Values
func setSSLMode(q url.Values) SSLMode {
sslMode := SSLDisable
if len(q["sslmode"]) > 0 && q["sslmode"][0] != "" {
sslMode = SSLMode(q["sslmode"][0])
}
return sslMode
}
// prettifyConnString prints out all the props from connection string in a neat
// way.
func prettifyConnString(conn string) string {
var str string
props := strings.Split(conn, " ")
sort.Strings(props)
for _, s := range props {
if s != "" {
str += fmt.Sprintf("\n\t%s", s)
}
}
return str
}