-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
95 lines (78 loc) · 2.32 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
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
package client
import (
"fmt"
"io/ioutil"
"time"
"github.com/lugondev/wallet-signer-manager/pkg/tls/certificate"
"github.com/go-pg/pg/v10"
"github.com/lugondev/wallet-signer-manager/pkg/tls"
)
const (
requireSSLMode = "require"
disableSSLMode = "disable"
verifyCASSLMode = "verify-ca"
verifyFullSSLMode = "verify-full"
)
type Config struct {
Host string `json:"host"`
Port string `json:"port"`
User string `json:"user"`
Password string `json:"password"`
Database string `json:"database"`
PoolSize int `json:"pool_size"`
PoolTimeout time.Duration `json:"pool_timeout"`
DialTimeout time.Duration `json:"dial_timeout"`
KeepAliveInterval time.Duration `json:"keep_alive_interval"`
ApplicationName string `json:"application_name"`
SSLMode string `json:"ssl_mode"`
TLSCert string `json:"tls_cert"`
TLSKey string `json:"tls_key"`
TLSCA string `json:"tls_ca"`
}
func (cfg *Config) ToPGOptions() (*pg.Options, error) {
opt := &pg.Options{
Addr: fmt.Sprintf("%v:%v", cfg.Host, cfg.Port),
User: cfg.User,
Password: cfg.Password,
Database: cfg.Database,
PoolSize: cfg.PoolSize,
ApplicationName: cfg.ApplicationName,
PoolTimeout: cfg.PoolTimeout,
}
tlsOption, err := cfg.getTLSOption()
if err != nil {
return nil, err
}
dialer, err := NewTLSDialer(cfg.SSLMode, cfg.Host, cfg.KeepAliveInterval, cfg.DialTimeout, tlsOption)
if err != nil {
return nil, err
}
if dialer != nil {
opt.Dialer = dialer.DialContext
} else {
opt.Dialer = Dialer(cfg.KeepAliveInterval, cfg.DialTimeout).DialContext
}
return opt, nil
}
func (cfg *Config) getTLSOption() (*tls.Option, error) {
tlsOption := &tls.Option{}
if cfg.TLSCert != "" && cfg.TLSKey != "" {
cert, err := ioutil.ReadFile(cfg.TLSCert)
if err != nil {
return nil, err
}
key, err := ioutil.ReadFile(cfg.TLSKey)
if err != nil {
return nil, err
}
tlsOption.Certificates = []*certificate.KeyPair{{Cert: cert, Key: key}}
if cfg.TLSCA != "" {
ca, err := ioutil.ReadFile(cfg.TLSCA)
if err != nil {
return nil, err
}
tlsOption.CAs = [][]byte{ca}
}
}
return tlsOption, nil
}