Skip to content

Commit

Permalink
feat(config): support load env variables as config (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode authored Sep 1, 2023
1 parent 9fdfb2a commit 5eddd9f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ArtalkJS/Artalk/internal/utils"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
)

Expand All @@ -29,6 +30,18 @@ func NewFromFile(cfgFile string) (*Config, error) {
return nil, fmt.Errorf("config file read error: %w", err)
}

// load environment variables and merge into the loaded config
const envPrefix = "ATK_"
if err := kf.Load(env.Provider(envPrefix, ".", func(s string) string {
// FOO__BAR -> foo_bar to handle dash in config names
s = strings.ToLower(strings.TrimPrefix(s, envPrefix))
s = strings.ReplaceAll(s, "__", "-")
s = strings.ReplaceAll(s, "_", ".")
return strings.ReplaceAll(s, "-", "_")
}), nil); err != nil {
return nil, fmt.Errorf("config environment variable parse error: %w", err)
}

// create new config instance
conf := &Config{
cfgFile: cfgFile,
Expand Down

0 comments on commit 5eddd9f

Please sign in to comment.