From ebfae9294fde12c9f8c7124d4c2589fc85060c64 Mon Sep 17 00:00:00 2001 From: Tibz-Dankan Date: Sat, 24 Aug 2024 13:05:45 +0300 Subject: [PATCH] refactor: get the db url basing on the environment --- Dockerfile | 2 ++ internal/config/db.go | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 49a9e83..36f4d0b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,8 @@ COPY . . RUN go build -o ./bin/appcrons ./cmd +ENV GO_ENV=production + EXPOSE 8080 CMD ["./bin/appcrons"] diff --git a/internal/config/db.go b/internal/config/db.go index 94d2556..3e910e7 100644 --- a/internal/config/db.go +++ b/internal/config/db.go @@ -10,7 +10,25 @@ import ( ) func Db() *gorm.DB { - dsn := os.Getenv("DSN_KEEP_ACTIVE") + var dsn string + + env := os.Getenv("GO_ENV") + log.Println("GO_ENV:", env) + + if env == "development" { + dsn = os.Getenv("APPCRONS_DEV_DSN") + + } else if env == "testing" { + dsn = os.Getenv("APPCRONS_TEST_DSN") + + } else if env == "staging" { + dsn = os.Getenv("APPCRONS_STAG_DSN") + + } else if env == "production" { + dsn = os.Getenv("APPCRONS_PROD_DSN") + } + + log.Println("dsn:", dsn) db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ SkipDefaultTransaction: true, PrepareStmt: true,