32 lines
578 B
Go
32 lines
578 B
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
_ "github.com/lib/pq"
|
|
"github.com/vinovest/sqlx"
|
|
)
|
|
|
|
var PostgresDatabase *sqlx.DB
|
|
|
|
func getDSN() string {
|
|
user := os.Getenv("PSQL_USER")
|
|
password := os.Getenv("PSQL_PASS")
|
|
database := os.Getenv("PSQL_NAME")
|
|
host, exists := os.LookupEnv("PSQL_HOST")
|
|
if !exists {
|
|
host = "localhost"
|
|
}
|
|
return fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable", user, password, host, database)
|
|
}
|
|
|
|
func ConnectPostgres() {
|
|
var err error
|
|
PostgresDatabase, err = sqlx.Open("postgres", getDSN())
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
}
|