@@ -12,14 +12,15 @@ import (
12
12
13
13
type (
14
14
// Config alias
15
- Config = struct {
16
- Addr string
17
- User string
15
+ Config struct {
16
+ Hostname string
17
+ Username string
18
18
Password string
19
19
Database string
20
20
Debug bool
21
21
PoolSize int
22
- Logger * zap.SugaredLogger
22
+ Logger * zap.Logger
23
+ Options map [string ]string
23
24
}
24
25
25
26
// Hook is a simple implementation of pg.QueryHook
@@ -28,8 +29,37 @@ type (
28
29
Before func (* pg.QueryEvent )
29
30
After func (* pg.QueryEvent )
30
31
}
32
+
33
+ // Error is constant error
34
+ Error string
31
35
)
32
36
37
+ const (
38
+ // ErrPemParse when couldn't parse pem in sslrootcert
39
+ ErrPemParse = Error ("couldn't parse pem in sslrootcert" )
40
+ // ErrEmptyConfig when given empty options
41
+ ErrEmptyConfig = Error ("database empty config" )
42
+ // ErrEmptyLogger when logger not initialized
43
+ ErrEmptyLogger = Error ("database empty logger" )
44
+ // ErrSSLKeyHasWorldPermissions when pk permissions no u=rw (0600) or less
45
+ ErrSSLKeyHasWorldPermissions = Error ("private key file has group or world access. Permissions should be u=rw (0600) or less" )
46
+
47
+ errUnsupportedSSLMode = `unsupported sslmode %q; only "require" (default), "verify-full", "verify-ca", and "disable" supported`
48
+ )
49
+
50
+ var (
51
+ // Module is default connection to PostgreSQL
52
+ Module = module.Module {
53
+ {Constructor : NewDefaultConfig },
54
+ {Constructor : NewConnection },
55
+ }
56
+ )
57
+
58
+ // Error implementation
59
+ func (e Error ) Error () string {
60
+ return string (e )
61
+ }
62
+
33
63
// BeforeQuery callback
34
64
func (h * Hook ) BeforeQuery (e * pg.QueryEvent ) {
35
65
h .StartAt = time .Now ()
@@ -50,38 +80,37 @@ func (h Hook) AfterQuery(e *pg.QueryEvent) {
50
80
h .After (e )
51
81
}
52
82
53
- var (
54
- // Module is default connection to PostgreSQL
55
- Module = module.Module {
56
- {Constructor : NewDefaultConfig },
57
- {Constructor : NewConnection },
58
- }
59
-
60
- // ErrEmptyConfig when given empty options
61
- ErrEmptyConfig = errors .New ("database empty config" )
62
- // ErrEmptyLogger when logger not initialized
63
- ErrEmptyLogger = errors .New ("database empty logger" )
64
- )
65
-
66
83
// NewDefaultConfig returns connection config
67
84
func NewDefaultConfig (v * viper.Viper ) (* Config , error ) {
68
85
if ! v .IsSet ("postgres" ) {
69
86
return nil , ErrEmptyConfig
70
87
}
71
88
89
+ // v.SetDefault("postgres.hostname", "localhost")
90
+ v .SetDefault ("postgres.options.sslmode" , "disable" )
91
+
92
+ // re-fetch by full key
93
+ options := v .GetStringMapString ("postgres.options" )
94
+ if len (options ) > 0 {
95
+ for opt := range options {
96
+ options [opt ] = v .GetString ("postgres.options." + opt )
97
+ }
98
+ }
99
+
72
100
return & Config {
73
- Addr : v .GetString ("postgres.address " ),
74
- User : v .GetString ("postgres.username" ),
101
+ Hostname : v .GetString ("postgres.hostname " ),
102
+ Username : v .GetString ("postgres.username" ),
75
103
Password : v .GetString ("postgres.password" ),
76
104
Database : v .GetString ("postgres.database" ),
77
105
Debug : v .GetBool ("postgres.debug" ),
78
106
PoolSize : v .GetInt ("postgres.pool_size" ),
107
+ Options : options ,
79
108
}, nil
80
109
}
81
110
82
111
// NewConnection returns database connection
83
- func NewConnection (opts * Config , l * zap.Logger ) (db * pg.DB , err error ) {
84
- if opts == nil {
112
+ func NewConnection (cfg * Config , l * zap.Logger , v * viper. Viper ) (db * pg.DB , err error ) {
113
+ if cfg == nil {
85
114
err = ErrEmptyConfig
86
115
return
87
116
}
@@ -91,26 +120,32 @@ func NewConnection(opts *Config, l *zap.Logger) (db *pg.DB, err error) {
91
120
return
92
121
}
93
122
123
+ opts := & pg.Options {
124
+ Addr : cfg .Hostname ,
125
+ User : cfg .Username ,
126
+ Password : cfg .Password ,
127
+ Database : cfg .Database ,
128
+ PoolSize : cfg .PoolSize ,
129
+ }
130
+
94
131
l .Debug ("Connect to PostgreSQL" ,
95
- zap .String ("address" , opts .Addr ),
96
- zap .String ("user" , opts .User ),
97
- zap .String ("password" , opts .Password ),
98
- zap .String ("database" , opts .Database ),
99
- zap .Int ("pool_size" , opts .PoolSize ))
100
-
101
- db = pg .Connect (& pg.Options {
102
- Addr : opts .Addr ,
103
- User : opts .User ,
104
- Password : opts .Password ,
105
- Database : opts .Database ,
106
- PoolSize : opts .PoolSize ,
107
- })
132
+ zap .String ("hostname" , cfg .Hostname ),
133
+ zap .String ("username" , cfg .Username ),
134
+ zap .String ("password" , cfg .Password ),
135
+ zap .String ("database" , cfg .Database ),
136
+ zap .Int ("pool_size" , cfg .PoolSize ),
137
+ zap .Any ("options" , cfg .Options ))
138
+
139
+ if opts .TLSConfig , err = ssl (cfg .Options ); err != nil {
140
+ return nil , err
141
+ }
108
142
143
+ db = pg .Connect (opts )
109
144
if _ , err = db .ExecOne ("SELECT 1" ); err != nil {
110
145
return nil , errors .Wrap (err , "can't connect to postgres" )
111
146
}
112
147
113
- if opts .Debug {
148
+ if cfg .Debug {
114
149
h := new (Hook )
115
150
h .After = func (e * pg.QueryEvent ) {
116
151
query , qErr := e .FormattedQuery ()
0 commit comments