Use simple connection string in config

This commit is contained in:
Jan Bader 2022-02-09 21:57:40 +00:00
parent 2c71c521f9
commit e934d407c2
3 changed files with 6 additions and 18 deletions

View File

@ -6,23 +6,13 @@ import (
// Config contains all needed configurations // Config contains all needed configurations
type Config struct { type Config struct {
DatabaseUser string DatabaseConnection string
DatabaseHost string
DatabasePassword string
DatabaseName string
} }
// LoadConfig from path // LoadConfig from path
func LoadConfig() (*Config, error) { func LoadConfig() (*Config, error) {
configuration := Config{ configuration := Config{
DatabaseUser: os.Getenv("BUDGETEER_DB_USER"), DatabaseConnection: os.Getenv("BUDGETEER_DB"),
DatabaseHost: os.Getenv("BUDGETEER_DB_HOST"),
DatabasePassword: os.Getenv("BUDGETEER_DB_PASS"),
DatabaseName: os.Getenv("BUDGETEER_DB_NAME"),
}
if configuration.DatabaseName == "" {
configuration.DatabaseName = "budgeteer"
} }
return &configuration, nil return &configuration, nil

View File

@ -18,15 +18,14 @@ services:
- ~/.go:/go - ~/.go:/go
- ~/.cache:/.cache - ~/.cache:/.cache
environment: environment:
BUDGETEER_DB_NAME: budgeteer BUDGETEER_DB: postgres://budgeteer:budgeteer@db:5432/budgeteer
BUDGETEER_DB_USER: budgeteer
BUDGETEER_DB_PASS: budgeteer
BUDGETEER_DB_HOST: db:5432
depends_on: depends_on:
- db - db
db: db:
image: postgres:14 image: postgres:14
ports:
- 5432:5432
volumes: volumes:
- db:/var/lib/postgresql/data - db:/var/lib/postgresql/data
environment: environment:

View File

@ -18,8 +18,7 @@ type Database struct {
} }
// Connect to a database // Connect to a database
func Connect(server string, user string, password string, database string) (*Database, error) { func Connect(connString string) (*Database, error) {
connString := fmt.Sprintf("postgres://%s:%s@%s/%s", user, password, server, database)
conn, err := sql.Open("pgx", connString) conn, err := sql.Open("pgx", connString)
if err != nil { if err != nil {
return nil, fmt.Errorf("open connection: %w", err) return nil, fmt.Errorf("open connection: %w", err)