POC-golang-postgres-docker/internal/app/db.go
2020-09-14 18:16:47 +05:00

50 lines
828 B
Go

package app
import (
"database/sql"
"fmt"
)
const (
dbhost = "DBHOST"
dbport = "DBPORT"
dbuser = "DBUSER"
dbpass = "DBPASS"
dbname = "DBNAME"
)
var Db *sql.DB
func InitDb() {
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)
Db, err = sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
err = Db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
}
func CloseDB() {
Db.Close()
}
func dbConfig() map[string]string {
conf := make(map[string]string)
conf[dbhost] = "genitywebapp_db_1"
conf[dbport] = "5432"
conf[dbuser] = "root_user"
conf[dbpass] = "shahzad12"
conf[dbname] = "db_genity"
return conf
}