-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_address.go
48 lines (40 loc) · 1.06 KB
/
generate_address.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
package pghelpers
import "fmt"
// GenerateAddress returns a string with DB connection information
func (c *PostgresConfig) GenerateAddress() string {
addr := fmt.Sprintf("host=%s port=%d dbname=%s",
c.Host,
c.Port,
c.Database,
)
if c.Username != "" {
addr = addr + fmt.Sprintf(" user=%s", c.Username)
}
if c.Password != "" {
addr = addr + fmt.Sprintf(" password=%s", c.Password)
}
if c.SSLEnabled {
addr = addr + " sslmode=require"
} else {
addr = addr + " sslmode=disable"
}
if c.ApplicationName != "" {
addr = addr + fmt.Sprintf(" application_name=%s", c.ApplicationName)
}
return addr
}
// URL returns a 'postgres://user:pass@host:port/database?sslmode=' style address
func (c PostgresConfig) URL() string {
var sslMode string
if c.SSLEnabled {
sslMode = "require"
} else {
sslMode = "disable"
}
url := fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?sslmode=%s",
c.Username, c.Password, c.Host, c.Port, c.Database, sslMode)
if c.ApplicationName != "" {
url += fmt.Sprintf("&fallback_application_name=%s", c.ApplicationName)
}
return url
}