-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
67 lines (60 loc) · 1.92 KB
/
config.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
package db
import (
"fmt"
"net/url"
"time"
)
type Config struct {
Type string `json:"type" validate:"nonzero"`
User string `json:"user" validate:"nonzero"`
Password string `json:"password"`
Host string `json:"host" validate:"nonzero"`
Port uint16 `json:"port" validate:"nonzero"`
Timeout int `json:"timeout" validate:"min=1"`
DatabaseName string `json:"database_name" validate:""`
RequireSSL bool `json:"require_ssl" validate:""`
CACert string `json:"ca_cert" validate:""`
SkipHostnameValidation bool `json:"skip_hostname_validation" validate:""`
}
func (c Config) ConnectionString() (string, error) {
if c.Timeout < 1 {
return "", fmt.Errorf("timeout must be at least 1 second: %d", c.Timeout)
}
switch c.Type {
case "postgres":
return buildPostgresConnectionString(c)
case "mysql":
mysqlConnectionStringBuilder := &MySQLConnectionStringBuilder{
MySQLAdapter: &MySQLAdapter{},
}
return mysqlConnectionStringBuilder.Build(c)
default:
return "", fmt.Errorf("database type '%s' is not supported", c.Type)
}
}
func buildPostgresConnectionString(c Config) (string, error) {
ms := (time.Duration(c.Timeout) * time.Second).Nanoseconds() / 1000 / 1000
sslmode := "disable"
params := url.Values{}
if c.RequireSSL {
if c.SkipHostnameValidation {
sslmode = "require"
} else {
if c.CACert == "" {
return "", fmt.Errorf("SSL is required but `CACert` is not provided")
}
sslmode = "verify-full"
params.Add("sslrootcert", c.CACert)
}
}
params.Add("sslmode", sslmode)
params.Add("connect_timeout", fmt.Sprintf("%d", ms))
connURL := url.URL{
Scheme: "postgres",
User: url.UserPassword(c.User, c.Password),
Host: fmt.Sprintf("%s:%d", c.Host, c.Port),
Path: c.DatabaseName,
RawQuery: params.Encode(),
}
return connURL.String(), nil
}