Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change the .env handler #15

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
}
Expand All @@ -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"]
Expand Down