-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add config loading from env with default option
* refactor: Move filepath creation to yaml redirect * feat: Add config helper
- Loading branch information
1 parent
ec9cffe
commit 44355c7
Showing
3 changed files
with
84 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package helper | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type Config struct { | ||
DebugLevel bool | ||
Host string | ||
Port string | ||
ReadTimeoutMS time.Duration | ||
WriteTimeoutMS time.Duration | ||
CacheTTLSec time.Duration | ||
YamlRedirectPath string | ||
} | ||
type EnvConfig struct { | ||
Config | ||
} | ||
|
||
func LoadConfigWithDefaults(cfg Config) *EnvConfig { | ||
c := Config{} | ||
dl, err := strconv.ParseBool(os.Getenv("DEBUG_LEVEL")) | ||
c.DebugLevel = dl | ||
if err != nil { | ||
c.DebugLevel = cfg.DebugLevel | ||
} | ||
h := os.Getenv("HOST") | ||
c.Host = h | ||
if h == "" { | ||
c.Host = cfg.Host | ||
} | ||
p := os.Getenv("PORT") | ||
c.Port = p | ||
if p == "" { | ||
c.Port = cfg.Port | ||
} | ||
rt, err := strconv.ParseInt(os.Getenv("READ_TIMEOUT_MS"), 10, 64) | ||
c.ReadTimeoutMS = time.Duration(rt) * time.Millisecond | ||
if err != nil { | ||
c.ReadTimeoutMS = cfg.ReadTimeoutMS | ||
} | ||
wt, err := strconv.ParseInt(os.Getenv("WRITE_TIMEOUT_MS"), 10, 64) | ||
c.WriteTimeoutMS = time.Duration(wt) * time.Millisecond | ||
if err != nil { | ||
c.WriteTimeoutMS = cfg.WriteTimeoutMS | ||
} | ||
ttl, err := strconv.ParseInt(os.Getenv("CACHE_TTL_SEC"), 10, 64) | ||
c.CacheTTLSec = time.Duration(ttl) * time.Second | ||
if err != nil { | ||
c.CacheTTLSec = cfg.CacheTTLSec | ||
} | ||
fp := os.Getenv("YAML_REDIRECT_PATH") | ||
if fp == "" { | ||
c.YamlRedirectPath = cfg.YamlRedirectPath | ||
} | ||
return &EnvConfig{ | ||
c, | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters