topic/cleaning #4
1
go.mod
1
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
|
||||
)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue