-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
83 lines (66 loc) · 2.14 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package config
import (
"encoding/json"
logPkg "log"
"regexp"
"strings"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/structs"
"github.com/mehditeymorian/hermes/internal/db"
"github.com/mehditeymorian/hermes/internal/emq"
"github.com/mehditeymorian/hermes/internal/log"
"github.com/tidwall/pretty"
"go.uber.org/zap"
)
const PREFIX = "HERMES_"
type Config struct {
Emq emq.Config `koanf:"emq"`
DB db.Config `koanf:"db"`
Logger log.Config `koanf:"logger"`
}
func Load(path string) Config {
var cfg Config
k := koanf.New(".")
// load default configuration
if err := k.Load(structs.Provider(Default(), "koanf"), nil); err != nil {
zap.L().Fatal("error loading default config", zap.Error(err))
}
// load configuration from file
if err := k.Load(file.Provider(path), yaml.Parser()); err != nil {
zap.L().Warn("error loading config.yaml", zap.Error(err))
}
// load environment variables
cb := func(key string, value string) (string, interface{}) {
finalKey := strings.ReplaceAll(strings.ToLower(strings.TrimPrefix(key, PREFIX)), "__", ".")
if strings.Contains(value, ",") {
// remove all the whitespace from value
// split the value using comma
finalValue := strings.Split(removeWhitespace(value), ",")
return finalKey, finalValue
}
return finalKey, value
}
if err := k.Load(env.ProviderWithValue(PREFIX, ".", cb), nil); err != nil {
zap.L().Warn("error loading environment variables", zap.Error(err))
}
if err := k.Unmarshal("", &cfg); err != nil {
zap.L().Fatal("error unmarshalling config", zap.Error(err))
}
indent, _ := json.MarshalIndent(cfg, "", "\t")
indent = pretty.Color(indent, nil)
cfgStrTemplate := `
================ Loaded Configuration ================
%s
======================================================
`
logPkg.Printf(cfgStrTemplate, string(indent))
return cfg
}
// removeWhitespace remove all the whitespaces from the input.
func removeWhitespace(in string) string {
compile := regexp.MustCompile(`\s+`)
return compile.ReplaceAllString(in, "")
}