Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sjonpaulbrown committed May 31, 2024
1 parent bea3b34 commit 23425ac
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion backend/main/server/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 23425ac

Please sign in to comment.