Load config from ENV
This commit is contained in:
		@@ -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{}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user