From 23425ac769bf47cd6e95c57179281c8039de6440 Mon Sep 17 00:00:00 2001 From: sjonpaulbrown Date: Fri, 31 May 2024 17:48:02 -0600 Subject: [PATCH] wip --- backend/main/server/app.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/backend/main/server/app.go b/backend/main/server/app.go index 099ce519..b8cac370 100644 --- a/backend/main/server/app.go +++ b/backend/main/server/app.go @@ -7,6 +7,7 @@ import ( "fmt" "io/ioutil" "net/http" + "net/url" "os" "strings" @@ -212,7 +213,7 @@ func (a *App) ConnectDB(username, password, host, port, dbname string) { database.Context = context.Background() database.Name = dbname - connectionString := fmt.Sprintf("postgres://%s:%s@%s/%s", username, password, host, dbname) + connectionString := ToUnixURL(false, username, password, dbname, host) pconf, confErr := pgxpool.ParseConfig(connectionString) if confErr != nil { @@ -235,3 +236,30 @@ func (a *App) ConnectDB(username, password, host, port, dbname string) { log.Info().Msgf("Successfully created Postgres conn pool") } } + +func ToUnixURL(ssl bool, username, password, db, host string) string { + urlStr := "postgresql://" + + if len(username) == 0 { + username = "postgres" + } + urlStr += username + + if len(password) > 0 { + urlStr = urlStr + ":" + url.PathEscape(password) + } + urlStr += "@" + + urlStr += "/" + url.PathEscape(db) + + // Append query parameters + urlStr += "?" + "host=" + host + + mode := "" + if !ssl { + mode = "&sslmode=disable" + } + + urlStr += mode + return urlStr +}