From 1ee48da79f4490f31a1605057d9ae3d375812462 Mon Sep 17 00:00:00 2001 From: Shaz hemani Date: Tue, 15 Sep 2020 14:53:57 +0500 Subject: [PATCH] Build generation and dockerfile changes --- Makefile | 2 ++ configs/web.env | 2 +- go.mod | 1 + internal/app/db.go | 44 ++++++++++++++++++++++++++++++++++---------- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 2bcdde3..2be1aff 100644 --- a/Makefile +++ b/Makefile @@ -7,4 +7,6 @@ run: build build-binary: GOOS=linux GOARCH=amd64 go build -o bins/genityapp -v cmd/* +Dev-run: build-binary build run + .PHONY: build-binary run build diff --git a/configs/web.env b/configs/web.env index e68a234..35d07fd 100644 --- a/configs/web.env +++ b/configs/web.env @@ -1,4 +1,4 @@ -dbhost=${COMPOSE_PROJECT_NAME}_db_1 +dbhost=genitywebapp_db_1 dbport=5432 dbuser=root_user dbpass=shahzad12 diff --git a/go.mod b/go.mod index f3d8b8e..4c7480a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module domain/genityapp go 1.14 require ( + github.com/joho/godotenv v1.3.0 github.com/lib/pq v1.8.0 github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d ) diff --git a/internal/app/db.go b/internal/app/db.go index 9cf4753..13472f9 100644 --- a/internal/app/db.go +++ b/internal/app/db.go @@ -3,14 +3,17 @@ package app import ( "database/sql" "fmt" + "github.com/joho/godotenv" + "log" + "os" ) const ( - dbhost = "DBHOST" - dbport = "DBPORT" - dbuser = "DBUSER" - dbpass = "DBPASS" - dbname = "DBNAME" + dbhost = "dbhost" + dbport = "dbport" + dbuser = "dbuser" + dbpass = "dbpass" + dbname = "dbname" ) var Db *sql.DB @@ -38,12 +41,33 @@ func CloseDB() { Db.Close() } +// return the value of the key +func goDotEnvVariable(key string) string { + + // load .env file + err := godotenv.Load("/go/src/app/configs/web.env") + + if err != nil { + log.Fatalf(err.Error()) + } + + return os.Getenv(key) +} + func dbConfig() map[string]string { + conf := make(map[string]string) - conf[dbhost] = "genitywebapp_db_1" - conf[dbport] = "5432" - conf[dbuser] = "root_user" - conf[dbpass] = "shahzad12" - conf[dbname] = "db_genity" + + user := goDotEnvVariable(dbuser) + host := goDotEnvVariable(dbhost) + port := goDotEnvVariable(dbport) + pass := goDotEnvVariable(dbpass) + databaseNname := goDotEnvVariable(dbname) + + conf[dbhost] = host + conf[dbport] = port + conf[dbuser] = user + conf[dbpass] = pass + conf[dbname] = databaseNname return conf }