bugfix: error instead of panic for invalid connection string

This commit is contained in:
AJ ONeal 2021-10-12 11:36:19 -06:00
parent b51f7992cd
commit 85147ca776
4 changed files with 28 additions and 22 deletions

View File

@ -143,15 +143,7 @@ func main() {
return
}
connStr := dbURL
// TODO url.Parse
if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") {
connStr += "?sslmode=disable"
} else {
connStr += "?sslmode=required"
}
store, err = authstore.NewStore(connStr, mgmt.InitSQL)
store, err = authstore.NewStore(dbURL, mgmt.InitSQL)
if nil != err {
log.Fatal("connection error", err)
return

View File

@ -10,10 +10,18 @@ import (
func main() {
connStr := "postgres://postgres:postgres@localhost:5432/postgres"
if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") {
connStr += "?sslmode=disable"
} else {
connStr += "?sslmode=required"
if !strings.Contains(connStr, "sslmode=") {
sep := "?"
if strings.Contains(connStr, sep) {
sep = "&"
}
if strings.Contains(connStr, "@localhost/") ||
strings.Contains(connStr, "@localhost:") {
connStr += sep + "sslmode=disable"
} else {
connStr += sep + "sslmode=required"
}
}
store, err := authstore.NewStore(connStr, initSQL)

View File

@ -1,14 +1,6 @@
package authstore
import "strings"
var connStr = "postgres://postgres:postgres@localhost/postgres"
func init() {
// TODO url.Parse
if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") {
connStr += "?sslmode=disable"
} else {
connStr += "?sslmode=required"
}
}

View File

@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"io/ioutil"
"strings"
"time"
"git.rootprojects.org/root/telebit/assets/files"
@ -16,9 +17,22 @@ import (
var initSQL = "./postgres.init.sql"
func NewStore(pgURL, initSQL string) (Store, error) {
func NewStore(dbURL, initSQL string) (Store, error) {
// https://godoc.org/github.com/lib/pq
// TODO url.Parse
if !strings.Contains(dbURL, "sslmode=") {
sep := "?"
if strings.Contains(connStr, sep) {
sep = "&"
}
if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") {
connStr += sep + "sslmode=disable"
} else {
connStr += sep + "sslmode=required"
}
}
f, err := files.Open(initSQL)
if nil != err {
return nil, err