feat(postgres): config map

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-24 12:59:51 +02:00
parent bfa074fd9b
commit b40d93c993
3 changed files with 27 additions and 6 deletions

View File

@ -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

View File

@ -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)

12
pkg/models/config.go Normal file
View File

@ -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"`
}