From e14470263f29d01651bf3200973b582f9ff3f797 Mon Sep 17 00:00:00 2001 From: Sergei Martynov Date: Tue, 19 Nov 2024 22:06:02 +0100 Subject: [PATCH] change the .env handler (#15) --- .env => .env.example | 0 .gitignore | 3 ++- internal/conf/conf.go | 15 ++++++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) rename .env => .env.example (100%) diff --git a/.env b/.env.example similarity index 100% rename from .env rename to .env.example diff --git a/.gitignore b/.gitignore index 78cfc25..6db5d87 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ bin/ *.log *.bak *.test -*.txt \ No newline at end of file +*.txt +.env diff --git a/internal/conf/conf.go b/internal/conf/conf.go index d814906..97b7afc 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -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"]