Skip to content

Commit

Permalink
adds db connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinGallauner committed Jul 5, 2024
1 parent 68d2d8d commit f1d1414
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions internal/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
)

type DatabaseConfig struct {
Host string `env:"POSTGRES_HOST" validate:"hostname"`
DbUser string `env:"POSTGRES_USER"`
Password string `env:"POSTGRES_PASSWORD"`
Dbname string `env:"POSTGRES_DBNAME"`
Port int `env:"POSTGRES_PORT"`
Sslmode string `env:"POSTGRES_SSLMODE"`
Timezone string `env:"TIMEZONE"`
Host string `env:"POSTGRES_HOST" validate:"hostname"`
DbUser string `env:"POSTGRES_USER"`
Password string `env:"POSTGRES_PASSWORD"`
Dbname string `env:"POSTGRES_DBNAME"`
Port int `env:"POSTGRES_PORT"`
Sslmode string `env:"POSTGRES_SSLMODE"`
Timezone string `env:"TIMEZONE"`
DatabaseUrl string `env:"DATABASE_URL"`
}

func ReadDatabaseConfig() (DatabaseConfig, error) {
Expand All @@ -34,9 +35,10 @@ func SetupDatabase(config DatabaseConfig) (*gorm.DB, error) {
if err != nil {
return nil, err
}
connString := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
config.Host, config.DbUser, config.Password, config.Dbname, config.Port, config.Sslmode, config.Timezone)
return SetupDatabaseWithDSN(connString)
// commented out to use the connection string provided by Digital Ocean
/* connString := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
config.Host, config.DbUser, config.Password, config.Dbname, config.Port, config.Sslmode, config.Timezone) */
return SetupDatabaseWithDSN(config.DatabaseUrl)
}

func SetupDatabaseWithDSN(connString string) (*gorm.DB, error) {
Expand Down

0 comments on commit f1d1414

Please sign in to comment.