Use simple connection string in config
This commit is contained in:
parent
2c71c521f9
commit
e934d407c2
@ -6,23 +6,13 @@ import (
|
||||
|
||||
// Config contains all needed configurations
|
||||
type Config struct {
|
||||
DatabaseUser string
|
||||
DatabaseHost string
|
||||
DatabasePassword string
|
||||
DatabaseName string
|
||||
DatabaseConnection string
|
||||
}
|
||||
|
||||
// LoadConfig from path
|
||||
func LoadConfig() (*Config, error) {
|
||||
configuration := Config{
|
||||
DatabaseUser: os.Getenv("BUDGETEER_DB_USER"),
|
||||
DatabaseHost: os.Getenv("BUDGETEER_DB_HOST"),
|
||||
DatabasePassword: os.Getenv("BUDGETEER_DB_PASS"),
|
||||
DatabaseName: os.Getenv("BUDGETEER_DB_NAME"),
|
||||
}
|
||||
|
||||
if configuration.DatabaseName == "" {
|
||||
configuration.DatabaseName = "budgeteer"
|
||||
DatabaseConnection: os.Getenv("BUDGETEER_DB"),
|
||||
}
|
||||
|
||||
return &configuration, nil
|
||||
|
@ -18,15 +18,14 @@ services:
|
||||
- ~/.go:/go
|
||||
- ~/.cache:/.cache
|
||||
environment:
|
||||
BUDGETEER_DB_NAME: budgeteer
|
||||
BUDGETEER_DB_USER: budgeteer
|
||||
BUDGETEER_DB_PASS: budgeteer
|
||||
BUDGETEER_DB_HOST: db:5432
|
||||
BUDGETEER_DB: postgres://budgeteer:budgeteer@db:5432/budgeteer
|
||||
depends_on:
|
||||
- db
|
||||
|
||||
db:
|
||||
image: postgres:14
|
||||
ports:
|
||||
- 5432:5432
|
||||
volumes:
|
||||
- db:/var/lib/postgresql/data
|
||||
environment:
|
||||
|
@ -18,8 +18,7 @@ type Database struct {
|
||||
}
|
||||
|
||||
// Connect to a database
|
||||
func Connect(server string, user string, password string, database string) (*Database, error) {
|
||||
connString := fmt.Sprintf("postgres://%s:%s@%s/%s", user, password, server, database)
|
||||
func Connect(connString string) (*Database, error) {
|
||||
conn, err := sql.Open("pgx", connString)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open connection: %w", err)
|
||||
|
Loading…
x
Reference in New Issue
Block a user