2020-09-14 13:16:47 +00:00
|
|
|
package app
|
2020-09-11 09:13:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dbhost = "DBHOST"
|
|
|
|
dbport = "DBPORT"
|
|
|
|
dbuser = "DBUSER"
|
|
|
|
dbpass = "DBPASS"
|
|
|
|
dbname = "DBNAME"
|
|
|
|
)
|
|
|
|
|
2020-09-14 13:16:47 +00:00
|
|
|
var Db *sql.DB
|
2020-09-11 09:13:41 +00:00
|
|
|
|
2020-09-14 13:16:47 +00:00
|
|
|
func InitDb() {
|
2020-09-11 09:13:41 +00:00
|
|
|
config := dbConfig()
|
|
|
|
var err error
|
|
|
|
psqlInfo := fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable",
|
|
|
|
config[dbuser], config[dbpass],
|
|
|
|
config[dbhost], config[dbport], config[dbname])
|
|
|
|
|
|
|
|
fmt.Println(psqlInfo)
|
2020-09-14 13:16:47 +00:00
|
|
|
Db, err = sql.Open("postgres", psqlInfo)
|
2020-09-11 09:13:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-09-14 13:16:47 +00:00
|
|
|
err = Db.Ping()
|
2020-09-11 09:13:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Successfully connected!")
|
|
|
|
}
|
|
|
|
|
2020-09-14 13:16:47 +00:00
|
|
|
func CloseDB() {
|
|
|
|
Db.Close()
|
|
|
|
}
|
|
|
|
|
2020-09-11 09:13:41 +00:00
|
|
|
func dbConfig() map[string]string {
|
|
|
|
conf := make(map[string]string)
|
2020-09-11 12:09:53 +00:00
|
|
|
conf[dbhost] = "genitywebapp_db_1"
|
2020-09-11 09:13:41 +00:00
|
|
|
conf[dbport] = "5432"
|
|
|
|
conf[dbuser] = "root_user"
|
|
|
|
conf[dbpass] = "shahzad12"
|
|
|
|
conf[dbname] = "db_genity"
|
|
|
|
return conf
|
|
|
|
}
|