Skip to content

Commit

Permalink
improve db env parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinGallauner committed Jul 2, 2024
1 parent ef1af5e commit d465c57
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/caarlos0/env/v11 v11.1.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.12 // indirect
github.com/containerd/log v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7fz8=
github.com/Microsoft/hcsshim v0.11.4/go.mod h1:smjE4dvqPX9Zldna+t5FG3rnoHhaB7QYxPRqGcpAD9w=
github.com/caarlos0/env/v11 v11.1.0 h1:a5qZqieE9ZfzdvbbdhTalRrHT5vu/4V1/ad1Ka6frhI=
github.com/caarlos0/env/v11 v11.1.0/go.mod h1:LwgkYk1kDvfGpHthrWWLof3Ny7PezzFwS4QrsJdHTMo=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/containerd/containerd v1.7.12 h1:+KQsnv4VnzyxWcfO9mlxxELaoztsDEjOuCMPAuPqgU0=
Expand Down
25 changes: 23 additions & 2 deletions internal/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,33 @@ package internal

import (
"fmt"
"github.com/caarlos0/env/v11"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

func SetupDatabase(host, dbUser, password, dbname, port, sslmode, timezone string) (*gorm.DB, error) {
connString := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s", host, dbUser, password, dbname, port, sslmode, timezone)
type DatabaseConfig struct {
Host string `env:"POSTGRES_HOST"`
DbUser string `env:"POSTGRES_USER"`
Password string `env:"POSTGRES_PASSWORD"`
Dbname string `env:"POSTGRES_DBNAME"`
Port string `env:"POSTGRES_PORT"`
Sslmode string `env:"POSTGRES_SSLMODE"`
Timezone string `env:"TIMEZONE"`
}

func ReadDatabaseConfig() (DatabaseConfig, error) {
var DbConfig DatabaseConfig
err := env.Parse(&DbConfig)
if err != nil {
return DatabaseConfig{}, err //consider returning pointer to struct
}
return DbConfig, nil
}

func SetupDatabase(config DatabaseConfig) (*gorm.DB, error) {
connString := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s",
config.Host, config.DbUser, config.Password, config.Dbname, config.Port, config.Sslmode, config.Timezone)
return SetupDatabaseWithDSN(connString)
}

Expand Down
14 changes: 5 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
repository "github.com/martingallauner/bookclub/internal/repository"
bcServer "github.com/martingallauner/bookclub/internal/server"
"log"
"os"
"time"
)

Expand All @@ -20,15 +19,12 @@ func main() {

auth.NewAuth()

host := os.Getenv("POSTGRES_HOST")
user := os.Getenv("POSTGRES_USER")
password := os.Getenv("POSTGRES_PASSWORD")
dbname := os.Getenv("POSTGRES_DBNAME")
port := os.Getenv("POSTGRES_PORT")
sslmode := os.Getenv("POSTGRES_SSLMODE")
timezone := os.Getenv("TIMEZONE")

db, err := internal.SetupDatabase(host, user, password, dbname, port, sslmode, timezone)
dbConfig, err := internal.ReadDatabaseConfig()
if err != nil {
log.Fatal(err)
}
db, err := internal.SetupDatabase(dbConfig)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit d465c57

Please sign in to comment.