From b40d93c993cbd4caf1ed7239a147cc002396187e Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 24 Jun 2024 12:59:51 +0200 Subject: [PATCH] feat(postgres): config map Signed-off-by: SoXX --- pkg/database/database.go | 3 ++- pkg/database/postgres.go | 18 +++++++++++++----- pkg/models/config.go | 12 ++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 pkg/models/config.go diff --git a/pkg/database/database.go b/pkg/database/database.go index cebec64..6db8e3c 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -2,12 +2,13 @@ package database import ( "context" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" ) type OtterSpace interface { // Connect establishes a connection to the database. - Connect(ctx context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error + Connect(ctx context.Context, config models.DatabaseConfig) error // CreateUserWithRelationToSource adds a user with a relation to a source. CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 0aac161..7c2371d 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -5,6 +5,10 @@ import ( "database/sql" "embed" "fmt" + log2 "log" + "os" + "time" + "git.dragse.it/anthrove/otter-space-sdk/internal/postgres" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" _ "github.com/lib/pq" @@ -13,9 +17,6 @@ import ( gormPostgres "gorm.io/driver/postgres" "gorm.io/gorm" logger2 "gorm.io/gorm/logger" - log2 "log" - "os" - "time" ) //go:embed migrations/*.sql @@ -33,8 +34,15 @@ func NewPostgresqlConnection(debugOutput bool) OtterSpace { } } -func (p *postgresqlConnection) Connect(_ context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error { - dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", endpoint, username, password, database, port, ssl, timezone) +func (p *postgresqlConnection) Connect(_ context.Context, config models.DatabaseConfig) error { + var localSSL string + if config.SSL { + localSSL = "require" + } else { + localSSL = "disable" + } + + dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone) var err error err = p.migrateDatabase(dsn) diff --git a/pkg/models/config.go b/pkg/models/config.go new file mode 100644 index 0000000..2013b95 --- /dev/null +++ b/pkg/models/config.go @@ -0,0 +1,12 @@ +package models + +type DatabaseConfig struct { + Endpoint string `env:"DB_ENDPOINT,required"` + Username string `env:"DB_USERNAME,required"` + Password string `env:"DB_PASSWORD,required,unset"` + Database string `env:"DB_DATABASE,required"` + Port int `env:"DB_PORT,required" envDefault:"5432"` + SSL bool `env:"DB_SSL,required" envDefault:"true"` + Timezone string `env:"DB_TIMEZONE,required" envDefault:"Europe/Berlin"` + Debug bool `env:"DB_DEBUG" envDefault:"false"` +}