Load config from ENV

This commit is contained in:
Jan Bader 2021-11-07 21:56:35 +01:00
parent 5518d57bbd
commit 2eb1457019
2 changed files with 10 additions and 13 deletions

View File

@ -10,9 +10,9 @@ import (
) )
func main() { func main() {
cfg, err := config.LoadConfig("config.json") cfg, err := config.LoadConfig()
if err != nil { if err != nil {
panic("Could not load Config from config.json") panic("Could not load Config")
} }
bv := &bcrypt.Verifier{} bv := &bcrypt.Verifier{}

View File

@ -1,7 +1,6 @@
package config package config
import ( import (
"encoding/json"
"os" "os"
) )
@ -11,21 +10,19 @@ type Config struct {
DatabaseHost string DatabaseHost string
DatabasePassword string DatabasePassword string
DatabaseName string DatabaseName string
file *os.File
} }
// LoadConfig from path // LoadConfig from path
func LoadConfig(path string) (*Config, error) { func LoadConfig() (*Config, error) {
file, err := os.Open(path) configuration := Config{
if err != nil { DatabaseUser: os.Getenv("BUDGETEER_DB_USER"),
return nil, err DatabaseHost: os.Getenv("BUDGETEER_DB_HOST"),
DatabasePassword: os.Getenv("BUDGETEER_DB_PASS"),
DatabaseName: os.Getenv("BUDGETEER_DB_NAME"),
} }
decoder := json.NewDecoder(file) if configuration.DatabaseName == "" {
configuration := Config{} configuration.DatabaseName = "budgeteer"
err = decoder.Decode(&configuration)
if err != nil {
return nil, err
} }
return &configuration, nil return &configuration, nil