Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Usage of pdd:
--database string Database name (default "postgres")
--user string Database user (default "postgres")
--pass string Database password (default "postgres")
--enable-ssl Use TSL/SSL for connection
--dial-timeout duration Dial timeout for establishing new connections (default 5s)
--read-timeout duration Timeout for socket reads. If reached, commands will fail (default 30s)
--max-retry int Maximum number of retries before giving up.
Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func main() {
flag.StringVar(&dbc.Database, "database", database.DefaultDatabase, "Database name")
flag.StringVar(&dbc.User, "user", database.DefaultUser, "Database user")
flag.StringVar(&dbc.Password, "pass", database.DefaultPassword, "Database password")
flag.BoolVar(&dbc.EnableSSL, "enable-ssl", false, "Use TSL/SSL for connection")
flag.DurationVar(&dbc.DialTimeout, "dial-timeout", database.DefaultDialTimeout, "Dial timeout for establishing new connections")
flag.DurationVar(&dbc.ReadTimeout, "read-timeout", database.DefaultReadTimeout, "Timeout for socket reads. If reached, commands will fail")
flag.IntVar(&dbc.MaxRetries, "max-retry", database.DefaultMaxRetries, "Maximum number of retries before giving up.")
Expand Down
1 change: 1 addition & 0 deletions database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
Database string
User string
Password string
EnableSSL bool
MaxRetries int
DialTimeout time.Duration
ReadTimeout time.Duration
Expand Down
9 changes: 9 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"context"
"crypto/tls"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -31,6 +32,13 @@ type db struct {

// ConnectDB connects to a database using provided options.
func ConnectDB(logger log.Logger, cfg *Config) (DB, error) {
var tlsConfig tls.Config

if cfg.EnableSSL {
serverName := strings.Split(cfg.Addr, ":")[0]
tlsConfig = tls.Config{ServerName: serverName}
}

pgdb := pg.Connect(
&pg.Options{
Addr: cfg.Addr,
Expand All @@ -40,6 +48,7 @@ func ConnectDB(logger log.Logger, cfg *Config) (DB, error) {
MaxRetries: cfg.MaxRetries,
DialTimeout: cfg.DialTimeout,
ReadTimeout: cfg.ReadTimeout,
TLSConfig: &tlsConfig,
},
)

Expand Down