topic/cleaning #4

Merged
shah merged 23 commits from topic/cleaning into master 2022-05-31 14:53:59 +00:00
2 changed files with 22 additions and 24 deletions
Showing only changes of commit 69a1fd6070 - Show all commits

1
go.mod
View file

@ -3,7 +3,6 @@ module domain/genityapp
go 1.14 go 1.14
require ( require (
github.com/joho/godotenv v1.3.0
github.com/lib/pq v1.8.0 github.com/lib/pq v1.8.0
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
) )

View file

@ -3,8 +3,6 @@ package app
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/joho/godotenv"
"log"
"os" "os"
) )
@ -41,34 +39,35 @@ func CloseDB() {
Db.Close() 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 { func dbConfig() map[string]string {
conf := make(map[string]string) conf := make(map[string]string)
user := goDotEnvVariable(dbuser) host, ok := os.LookupEnv(dbhost)
host := goDotEnvVariable(dbhost) if !ok {
port := goDotEnvVariable(dbport) panic("DBHOST environment variable required but not set")
pass := goDotEnvVariable(dbpass) }
databaseNname := goDotEnvVariable(dbname) 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[dbhost] = host
conf[dbport] = port conf[dbport] = port
conf[dbuser] = user conf[dbuser] = user
conf[dbpass] = pass conf[dbpass] = password
conf[dbname] = databaseNname conf[dbname] = name
return conf return conf
} }