Skip to content

Commit

Permalink
Cleanup postgres connection variables (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaanqui authored May 2, 2024
1 parent eeb04bc commit 35e6ed2
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions database/db_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,14 @@ func DbConnectionString(options DbConnection) string {
return dns
}

func DbConfig() (DbConnection, error) {
var db_config DbConnection
err := utils.FileMapper("db_config.json", &db_config)
if os.Getenv("PGENV") != "local" {
return ProdDbConfig(), nil
}
// This method should only be used for local development
func DbConfig() (db_config DbConnection, err error) {
err = utils.FileMapper("db_config.json", &db_config)
return db_config, err
}

func ProdDbConfig() DbConnection {
port, err := strconv.Atoi(os.Getenv("PGPORT"))
if err != nil {
panic("Error establishing DB connection, no port found.")
}
return DbConnection{
Host: os.Getenv("PGHOST"),
DbName: os.Getenv("PGDATABASE"),
Username: os.Getenv("POSTGRES_USER"),
Password: os.Getenv("PGPASSWORD"),
Port: uint16(port),
}
}

func CreateDbConnection(options DbConnection) (*sql.DB, error) {
if db_url, exists := os.LookupEnv("PG_PRIVATE_DATABASE_URL"); exists {
if db_url, exists := os.LookupEnv("PG_DATABASE_URL"); exists {
return sql.Open("postgres", db_url)
}
return sql.Open("postgres", DbConnectionString(options))
Expand Down

0 comments on commit 35e6ed2

Please sign in to comment.