Skip to content

Commit

Permalink
add configurable options for ping check and sslmode
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinkhoo committed Jan 21, 2025
1 parent 81b0eb6 commit 647a15d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ port="5432"
user="postgres"
dbname="postgres"
password="xxxxx"
sslmode="disable"
maxIdleConn = 2
maxOpenConn = 2
[app]
debug = true
pingCheck = true
```

Expand Down
1 change: 1 addition & 0 deletions kshieldconfig_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# port = "5432"
# user = "postgres"
# password = "password123"
# sslmode = "disable"
# dbname = "mydb"
# maxIdleConn = 10
# maxOpenConn = 100
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ type GeneratePassword struct {
type App struct {
Debug bool `toml:"debug"`
Hostname string `toml:"hostname"`
PingCheck bool `toml:"pingCheck"`
Run bool
RunPostgres bool
RunMySql bool
Expand Down
11 changes: 7 additions & 4 deletions pkg/mysqldb/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ func Open(conf config.MySQL) (*sql.DB, string, error) {
Msg("Failed to connect to database")
return nil, "", err
}
err = db.Ping()
if err != nil {
// fmt.Printf("Failed to connect to database. Error: %s", err.Error())
return nil, "", err

if cnf.App.PingCheck {
err = db.Ping()
if err != nil {
// fmt.Printf("Failed to connect to database. Error: %s", err.Error())
return nil, "", err
}
}
if conf.MaxIdleConn > 0 {
db.SetMaxIdleConns(conf.MaxIdleConn)
Expand Down
22 changes: 12 additions & 10 deletions pkg/postgresdb/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Postgres struct {
User string `toml:"user"`
Password string `toml:"password"`
DBName string `toml:"dbname"`
// SSLmode string `toml:"sslmode"`
SSLmode string `toml:"sslmode"`
MaxIdleConn int `toml:"maxIdleConn"`
MaxOpenConn int `toml:"maxOpenConn"`
}
Expand All @@ -35,7 +35,7 @@ var re = regexp.MustCompile(`(?m)(?:host=)([^\s]+)`)

func Open(conf Postgres) (*sql.DB, string, error) {
// "host=localhost port=5432 user=postgres password=postgres dbname=postgres sslmode=disable"
url := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", conf.Host, conf.Port, conf.User, conf.Password, conf.DBName)
url := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", conf.Host, conf.Port, conf.User, conf.Password, conf.DBName)

db, err := ConnectDatabaseUsingConnectionString(url)
if err != nil {
Expand Down Expand Up @@ -78,14 +78,16 @@ func ConnectDatabaseUsingConnectionString(url string) (*sql.DB, error) {
return nil, err
}

err = db.Ping()
if err != nil {
log.Error().
Err(err).
Str("conn", url).
Msg("Failed to ping database")
db.Close()
return nil, err
if cnf.App.PingCheck {
err = db.Ping()
if err != nil {
log.Error().
Err(err).
Str("conn", url).
Msg("Failed to ping database")
db.Close()
return nil, err
}
}

return db, nil
Expand Down

0 comments on commit 647a15d

Please sign in to comment.