mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
feat(database): connect postgres via socket and read config from env _FILE secrets (#2061)
* feat(database): connect postgres via socket * feat(config): read env var secrets from file * docs: explain env var secrets * refactor: generate postgres dsn
This commit is contained in:
parent
24648e45f7
commit
fe4f385a22
9 changed files with 345 additions and 76 deletions
|
@ -6,8 +6,8 @@ package database
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
@ -35,13 +35,33 @@ type DB struct {
|
|||
|
||||
func NewDB(cfg *domain.Config, log logger.Logger) (*DB, error) {
|
||||
db := &DB{
|
||||
// set default placeholder for squirrel to support both sqlite and postgres
|
||||
// set a default placeholder for squirrel to support both sqlite and postgres
|
||||
squirrel: sq.StatementBuilder.PlaceholderFormat(sq.Dollar),
|
||||
log: log.With().Str("module", "database").Str("type", cfg.DatabaseType).Logger(),
|
||||
cfg: cfg,
|
||||
}
|
||||
db.ctx, db.cancel = context.WithCancel(context.Background())
|
||||
|
||||
// Check for directly configured DSN in config
|
||||
if cfg.DatabaseDSN != "" {
|
||||
if strings.HasPrefix(cfg.DatabaseDSN, "postgres://") || strings.HasPrefix(cfg.DatabaseDSN, "postgresql://") {
|
||||
db.Driver = "postgres"
|
||||
db.DSN = cfg.DatabaseDSN
|
||||
return db, nil
|
||||
} else if strings.HasPrefix(cfg.DatabaseDSN, "file:") || cfg.DatabaseDSN == ":memory:" || strings.HasSuffix(cfg.DatabaseDSN, ".db") {
|
||||
db.Driver = "sqlite"
|
||||
if strings.HasPrefix(cfg.DatabaseDSN, "file:") && strings.HasSuffix(cfg.DatabaseDSN, ".db") {
|
||||
db.DSN = strings.TrimPrefix(cfg.DatabaseDSN, "file:")
|
||||
} else {
|
||||
db.DSN = cfg.DatabaseDSN
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
return nil, errors.New("unsupported database DSN: %s", cfg.DatabaseDSN)
|
||||
}
|
||||
|
||||
// If no direct DSN is provided, build it from individual settings
|
||||
switch cfg.DatabaseType {
|
||||
case "sqlite":
|
||||
db.Driver = "sqlite"
|
||||
|
@ -51,14 +71,19 @@ func NewDB(cfg *domain.Config, log logger.Logger) (*DB, error) {
|
|||
db.DSN = dataSourceName(cfg.ConfigPath, "autobrr.db")
|
||||
}
|
||||
case "postgres":
|
||||
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=%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"
|
||||
|
||||
// If no database-specific settings are provided, return an error
|
||||
if cfg.PostgresDatabase == "" && cfg.DatabaseDSN == "" {
|
||||
return nil, errors.New("postgres: database name is required")
|
||||
}
|
||||
|
||||
pgDsn, err := PostgresDSN(cfg.PostgresHost, cfg.PostgresPort, cfg.PostgresUser, cfg.PostgresPass, cfg.PostgresDatabase, cfg.PostgresSocket, cfg.PostgresSSLMode, cfg.PostgresExtraParams)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "postgres: failed to build DSN")
|
||||
}
|
||||
db.DSN = pgDsn
|
||||
|
||||
default:
|
||||
return nil, errors.New("unsupported database: %v", cfg.DatabaseType)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue