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