From 2eb1457019040af2631baec48d3cb26fc77521ea Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Sun, 7 Nov 2021 21:56:35 +0100 Subject: [PATCH] Load config from ENV --- cmd/budgeteer/main.go | 4 ++-- config/config.go | 19 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/cmd/budgeteer/main.go b/cmd/budgeteer/main.go index cf1cf94..21977c7 100644 --- a/cmd/budgeteer/main.go +++ b/cmd/budgeteer/main.go @@ -10,9 +10,9 @@ import ( ) func main() { - cfg, err := config.LoadConfig("config.json") + cfg, err := config.LoadConfig() if err != nil { - panic("Could not load Config from config.json") + panic("Could not load Config") } bv := &bcrypt.Verifier{} diff --git a/config/config.go b/config/config.go index 5d8f0df..200be74 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,6 @@ package config import ( - "encoding/json" "os" ) @@ -11,21 +10,19 @@ type Config struct { DatabaseHost string DatabasePassword string DatabaseName string - file *os.File } // LoadConfig from path -func LoadConfig(path string) (*Config, error) { - file, err := os.Open(path) - if err != nil { - return nil, err +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"), } - decoder := json.NewDecoder(file) - configuration := Config{} - err = decoder.Decode(&configuration) - if err != nil { - return nil, err + if configuration.DatabaseName == "" { + configuration.DatabaseName = "budgeteer" } return &configuration, nil