From 73e76c4214261c3eeb1c6f189d7c44d86ac95788 Mon Sep 17 00:00:00 2001 From: Antoine Date: Mon, 6 Nov 2023 19:04:32 +0100 Subject: [PATCH] feat(database): postgres set ssl mode (#1245) * feat(database): postgres set ssl mode * feat(database): postgres set extra params --- internal/config/config.go | 36 +++++++++++++++++---------------- internal/database/database.go | 5 ++++- internal/domain/config.go | 38 ++++++++++++++++++----------------- 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 263edc2..d8e2af8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -182,23 +182,25 @@ func New(configPath string, version string) *AppConfig { func (c *AppConfig) defaults() { c.Config = &domain.Config{ - Version: "dev", - Host: "localhost", - Port: 7474, - LogLevel: "TRACE", - LogPath: "", - LogMaxSize: 50, - LogMaxBackups: 3, - BaseURL: "/", - SessionSecret: api.GenerateSecureToken(16), - CustomDefinitions: "", - CheckForUpdates: true, - DatabaseType: "sqlite", - PostgresHost: "", - PostgresPort: 0, - PostgresDatabase: "", - PostgresUser: "", - PostgresPass: "", + Version: "dev", + Host: "localhost", + Port: 7474, + LogLevel: "TRACE", + LogPath: "", + LogMaxSize: 50, + LogMaxBackups: 3, + BaseURL: "/", + SessionSecret: api.GenerateSecureToken(16), + CustomDefinitions: "", + CheckForUpdates: true, + DatabaseType: "sqlite", + PostgresHost: "", + PostgresPort: 0, + PostgresDatabase: "", + PostgresUser: "", + PostgresPass: "", + PostgresSSLMode: "disable", + PostgresExtraParams: "", } } diff --git a/internal/database/database.go b/internal/database/database.go index 93c9351..dac3f1b 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -49,7 +49,10 @@ func NewDB(cfg *domain.Config, log logger.Logger) (*DB, error) { if cfg.PostgresHost == "" || cfg.PostgresPort == 0 || cfg.PostgresDatabase == "" { return nil, errors.New("postgres: bad variables") } - db.DSN = fmt.Sprintf("postgres://%v:%v@%v:%d/%v?sslmode=disable", cfg.PostgresUser, cfg.PostgresPass, cfg.PostgresHost, cfg.PostgresPort, cfg.PostgresDatabase) + db.DSN = fmt.Sprintf("postgres://%v:%v@%v:%d/%v?sslmode=%v", cfg.PostgresUser, cfg.PostgresPass, cfg.PostgresHost, cfg.PostgresPort, cfg.PostgresDatabase, cfg.PostgresSSLMode) + if cfg.PostgresExtraParams != "" { + db.DSN = fmt.Sprintf("%s&%s", db.DSN, cfg.PostgresExtraParams) + } db.Driver = "postgres" databaseDriver = "postgres" default: diff --git a/internal/domain/config.go b/internal/domain/config.go index 0a16136..256a6a6 100644 --- a/internal/domain/config.go +++ b/internal/domain/config.go @@ -4,24 +4,26 @@ package domain type Config struct { - Version string - ConfigPath string - Host string `toml:"host"` - Port int `toml:"port"` - LogLevel string `toml:"logLevel"` - LogPath string `toml:"logPath"` - LogMaxSize int `toml:"logMaxSize"` - LogMaxBackups int `toml:"logMaxBackups"` - BaseURL string `toml:"baseUrl"` - SessionSecret string `toml:"sessionSecret"` - CustomDefinitions string `toml:"customDefinitions"` - CheckForUpdates bool `toml:"checkForUpdates"` - DatabaseType string `toml:"databaseType"` - PostgresHost string `toml:"postgresHost"` - PostgresPort int `toml:"postgresPort"` - PostgresDatabase string `toml:"postgresDatabase"` - PostgresUser string `toml:"postgresUser"` - PostgresPass string `toml:"postgresPass"` + Version string + ConfigPath string + Host string `toml:"host"` + Port int `toml:"port"` + LogLevel string `toml:"logLevel"` + LogPath string `toml:"logPath"` + LogMaxSize int `toml:"logMaxSize"` + LogMaxBackups int `toml:"logMaxBackups"` + BaseURL string `toml:"baseUrl"` + SessionSecret string `toml:"sessionSecret"` + CustomDefinitions string `toml:"customDefinitions"` + CheckForUpdates bool `toml:"checkForUpdates"` + DatabaseType string `toml:"databaseType"` + PostgresHost string `toml:"postgresHost"` + PostgresPort int `toml:"postgresPort"` + PostgresDatabase string `toml:"postgresDatabase"` + PostgresUser string `toml:"postgresUser"` + PostgresPass string `toml:"postgresPass"` + PostgresSSLMode string `toml:"postgresSSLMode"` + PostgresExtraParams string `toml:"PostgresExtraParams"` } type ConfigUpdate struct {