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 return
} }
connStr := dbURL store, err = authstore.NewStore(dbURL, mgmt.InitSQL)
// 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)
if nil != err { if nil != err {
log.Fatal("connection error", err) log.Fatal("connection error", err)
return return

View File

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

View File

@ -1,14 +1,6 @@
package authstore package authstore
import "strings"
var connStr = "postgres://postgres:postgres@localhost/postgres" var connStr = "postgres://postgres:postgres@localhost/postgres"
func init() { 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" "database/sql"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings"
"time" "time"
"git.rootprojects.org/root/telebit/assets/files" "git.rootprojects.org/root/telebit/assets/files"
@ -16,9 +17,22 @@ import (
var initSQL = "./postgres.init.sql" 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 // 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) f, err := files.Open(initSQL)
if nil != err { if nil != err {
return nil, err return nil, err