Skip to content

Commit 8638c61

Browse files
committed
detect DATABASE_URL that consist of postgres
1 parent 3dcf9b1 commit 8638c61

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
############################
22
# STEP 1 build executable binary
33
############################
4-
FROM golang:alpine AS builder
4+
FROM golang:1.19.10-alpine3.18 AS builder
55
RUN apk update && apk add --no-cache git gcc libc-dev openssl && go install github.com/gobuffalo/packr/packr@latest
66
WORKDIR $GOPATH/src/securitybunker/databunker/src/
77
COPY src/go.mod ./deps

src/storage/pgsql-storage.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"log"
1010
"net/http"
11+
"net/url"
1112
"os"
1213
"strconv"
1314
"strings"
@@ -24,6 +25,22 @@ type PGSQLDB struct {
2425
}
2526

2627
func (dbobj PGSQLDB) getConnectionString(dbname *string) string {
28+
databaseURL := os.Getenv("DATABASE_URL")
29+
if len(databaseURL) > 0 {
30+
u, err := url.Parse(databaseURL)
31+
if err == nil && u.Scheme == "postgres" {
32+
// Extract user info, host, port, and dbname from the URL
33+
user := u.User.Username()
34+
pass, _ := u.User.Password()
35+
host := u.Hostname()
36+
port := u.Port()
37+
dbname := strings.TrimPrefix(u.Path, "/")
38+
39+
return fmt.Sprintf("user='%s' password='%s' host='%s' port='%s' dbname='%s'",
40+
user, pass, host, port, dbname)
41+
}
42+
}
43+
2744
user := os.Getenv("PGSQL_USER_NAME")
2845
pass := os.Getenv("PGSQL_USER_PASS")
2946
host := os.Getenv("PGSQL_HOST")

src/storage/storage.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package storage
33
import (
44
"go.mongodb.org/mongo-driver/bson"
55
"net/http"
6+
"net/url"
67
"os"
78
)
89

@@ -104,8 +105,18 @@ type BackendDB interface {
104105
}
105106

106107
func getDBObj() BackendDB {
107-
host := os.Getenv("MYSQL_HOST")
108108
var db BackendDB
109+
databaseURL := os.Getenv("DATABASE_URL")
110+
// Check if DATABASE_URL is set and is a PostgreSQL URL
111+
if len(databaseURL) > 0 {
112+
u, err := url.Parse(databaseURL)
113+
if err == nil && u.Scheme == "postgres" {
114+
db = &PGSQLDB{}
115+
return db
116+
}
117+
}
118+
119+
host := os.Getenv("MYSQL_HOST")
109120
if len(host) > 0 {
110121
db = &MySQLDB{}
111122
return db

0 commit comments

Comments
 (0)