Skip to content

Commit

Permalink
change the .env handler (#15)
Browse files Browse the repository at this point in the history
sgmv authored Nov 19, 2024
1 parent 7e6eddc commit e144702
Showing 3 changed files with 12 additions and 6 deletions.
File renamed without changes.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -7,4 +7,5 @@ bin/
*.log
*.bak
*.test
*.txt
*.txt
.env
15 changes: 10 additions & 5 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
@@ -33,19 +33,21 @@ func (c *Config) Validate() error {
if p < 1024 || p > 50000 {
return fmt.Errorf("wrong port for http server")
}

if c.LogLevel == "" {
c.LogLevel = DevelopMode
}
return nil
}

// LoadConf loads configuration from .env file and environment.
// Env variables are preferred.
func LoadConf() (*Config, error) {
envMap, err := godotenv.Read()
if err != nil {
return nil, err
}
var envMap map[string]string
envMap, _ = godotenv.Read()

var c Config
err = envconfig.Process(osPref, &c)
err := envconfig.Process(osPref, &c)
if err != nil {
return nil, err
}
@@ -60,6 +62,9 @@ func LoadConf() (*Config, error) {
}

func mergeConfigs(env map[string]string, c *Config) {
if env == nil {
return
}
// TODO: use reflect to automate it
if c.DB == "" {
v, ok := env["SD_DB"]

0 comments on commit e144702

Please sign in to comment.