From 69a1fd6070e119209ae6ec4d74fe63dec81e51d2 Mon Sep 17 00:00:00 2001 From: Shaz hemani Date: Tue, 15 Sep 2020 17:59:37 +0500 Subject: [PATCH] env read from OS env --- go.mod | 1 - internal/app/db.go | 45 ++++++++++++++++++++++----------------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 4c7480a..f3d8b8e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ 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 1660da6..f53d291 100644 --- a/internal/app/db.go +++ b/internal/app/db.go @@ -3,8 +3,6 @@ package app import ( "database/sql" "fmt" - "github.com/joho/godotenv" - "log" "os" ) @@ -41,34 +39,35 @@ func CloseDB() { Db.Close() } -// return the value of the key -func goDotEnvVariable(key string) string { - - // load .env file - pwd, _ := os.Getwd() - err := godotenv.Load(pwd + "/configs/web.env") - - if err != nil { - log.Fatalf(err.Error()) - } - - return os.Getenv(key) -} - func dbConfig() map[string]string { conf := make(map[string]string) - user := goDotEnvVariable(dbuser) - host := goDotEnvVariable(dbhost) - port := goDotEnvVariable(dbport) - pass := goDotEnvVariable(dbpass) - databaseNname := goDotEnvVariable(dbname) + host, ok := os.LookupEnv(dbhost) + if !ok { + panic("DBHOST environment variable required but not set") + } + port, ok := os.LookupEnv(dbport) + if !ok { + panic("DBPORT environment variable required but not set") + } + user, ok := os.LookupEnv(dbuser) + if !ok { + panic("DBUSER environment variable required but not set") + } + password, ok := os.LookupEnv(dbpass) + if !ok { + panic("DBPASS environment variable required but not set") + } + name, ok := os.LookupEnv(dbname) + if !ok { + panic("DBNAME environment variable required but not set") + } conf[dbhost] = host conf[dbport] = port conf[dbuser] = user - conf[dbpass] = pass - conf[dbname] = databaseNname + conf[dbpass] = password + conf[dbname] = name return conf }