From d465c57ba24ba93158d705434b365702dc76be33 Mon Sep 17 00:00:00 2001 From: Martin Gallauner Date: Tue, 2 Jul 2024 16:41:23 +0200 Subject: [PATCH] improve db env parsing --- go.mod | 1 + go.sum | 2 ++ internal/database.go | 25 +++++++++++++++++++++++-- main.go | 14 +++++--------- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 01d746c..95a74f7 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c62732e..c1bbab8 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/database.go b/internal/database.go index 66acb21..bcefb0a 100644 --- a/internal/database.go +++ b/internal/database.go @@ -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) } diff --git a/main.go b/main.go index 6b97242..4cfb5a5 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,6 @@ import ( repository "github.com/martingallauner/bookclub/internal/repository" bcServer "github.com/martingallauner/bookclub/internal/server" "log" - "os" "time" ) @@ -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) }