Skip to content

Commit e9959f6

Browse files
committed
added templating for configs
**testing**
1 parent f3f7be1 commit e9959f6

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

config/defaults.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
server:
22
port: 8880
33

4-
api:
5-
tokens:
6-
url:
4+
logLevel: "INFO"
75

86
messageAliases:
97
[
@@ -26,6 +24,10 @@ messageAliases:
2624
{ alias: data, score: 1 },
2725
]
2826

27+
variables:
28+
recipients: ${RECIPIENTS}
29+
number: ${NUMBER}
30+
2931
blockedEndpoints:
3032
- /v1/about
3133
- /v1/configuration

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ func main() {
2525

2626
ENV = config.ENV
2727

28+
logLevel = ENV.LOG_LEVEL
29+
30+
if logLevel != "" {
31+
log.Init(logLevel)
32+
}
33+
2834
initHandler = proxy.Create(ENV.API_URL)
2935

3036
body_m4 := middlewares.BodyMiddleware{

utils/config/config.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type ENV_ struct {
2222
CONFIG_PATH string
2323
DEFAULTS_PATH string
2424
TOKENS_DIR string
25+
LOG_LEVEL string
2526
PORT string
2627
API_URL string
2728
API_TOKENS []string
@@ -49,6 +50,8 @@ var config *koanf.Koanf
4950

5051
func InitEnv() {
5152
ENV.PORT = strconv.Itoa(config.Int("server.port"))
53+
54+
ENV.LOG_LEVEL = config.String("loglevel")
5255

5356
ENV.API_URL = config.String("api.url")
5457

@@ -77,10 +80,12 @@ func InitEnv() {
7780
}
7881

7982
config.Unmarshal("messagealiases", &ENV.MESSAGE_ALIASES)
80-
config.Unmarshal("variables", &ENV.VARIABLES)
8183

82-
ENV.VARIABLES["NUMBER"] = config.String("number")
83-
ENV.VARIABLES["RECIPIENTS"] = config.Strings("recipients")
84+
transformChildren(config, "variables", func(key string, value any) (string, any) {
85+
return strings.ToUpper(key), value
86+
})
87+
88+
config.Unmarshal("variables", &ENV.VARIABLES)
8489

8590
ENV.BLOCKED_ENDPOINTS = config.Strings("blockedendpoints")
8691
}
@@ -114,6 +119,8 @@ func Load() {
114119

115120
normalizeKeys(config)
116121

122+
templateConfig(config)
123+
117124
InitEnv()
118125

119126
log.Info("Finished Loading Configuration")
@@ -141,6 +148,20 @@ func LoadFile(path string, config *koanf.Koanf, parser koanf.Parser) (koanf.Prov
141148
return f, err
142149
}
143150

151+
func templateConfig(config *koanf.Koanf) {
152+
data := config.All()
153+
154+
for key, value := range data {
155+
str, isStr := value.(string)
156+
157+
if isStr {
158+
data[key] = os.ExpandEnv(str)
159+
}
160+
}
161+
162+
config.Load(confmap.Provider(data, "."), nil)
163+
}
164+
144165
func LoadEnv(config *koanf.Koanf) (koanf.Provider, error) {
145166
e := env.Provider(".", env.Opt{
146167
TransformFunc: normalizeEnv,
@@ -175,8 +196,36 @@ func normalizeKeys(config *koanf.Koanf) {
175196
config.Load(confmap.Provider(data, "."), nil)
176197
}
177198

199+
func transformChildren(config *koanf.Koanf, prefix string, transform func(key string, value any) (string, any)) error {
200+
var sub map[string]any
201+
if err := config.Unmarshal(prefix, &sub); err != nil {
202+
return err
203+
}
204+
205+
transformed := make(map[string]any)
206+
for key, val := range sub {
207+
newKey, newVal := transform(key, val)
208+
209+
transformed[newKey] = newVal
210+
}
211+
212+
// Remove the old subtree by overwriting with empty map
213+
config.Load(confmap.Provider(map[string]any{
214+
prefix: map[string]any{},
215+
}, "."), nil)
216+
217+
// Load the normalized subtree back in
218+
config.Load(confmap.Provider(map[string]any{
219+
prefix: transformed,
220+
}, "."), nil)
221+
222+
return nil
223+
}
224+
178225
func normalizeEnv(key string, value string) (string, any) {
179-
key = strings.ToLower(strings.ReplaceAll(key, "__", "."))
226+
key = strings.ToLower(key)
227+
key = strings.ReplaceAll(key, "__", ".")
228+
key = strings.ReplaceAll(key, "_", "")
180229

181230
return key, safestrings.ToType(value)
182231
}

0 commit comments

Comments
 (0)