forked from grassrootseconomics/visedriver
75 lines
2.5 KiB
Go
75 lines
2.5 KiB
Go
//go:build configreport
|
|
|
|
package config
|
|
|
|
import (
|
|
"git.defalsify.org/vise.git/logging"
|
|
"git.grassecon.net/grassrootseconomics/visedriver/env"
|
|
)
|
|
|
|
// ConfigValue represents a configuration key-value pair
|
|
type ConfigValue struct {
|
|
Key string
|
|
ValueFunc func() string
|
|
Sensitive bool
|
|
}
|
|
|
|
// Reporter handles configuration reporting
|
|
type Reporter struct {
|
|
values []ConfigValue
|
|
logger logging.Vanilla
|
|
}
|
|
|
|
func NewReporter(logger logging.Vanilla) *Reporter {
|
|
return &Reporter{
|
|
values: defaultConfigValues(),
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func defaultConfigValues() []ConfigValue {
|
|
return []ConfigValue{
|
|
{"HOST", func() string { return env.GetEnv("HOST", "127.0.0.1") }, false},
|
|
{"PORT", func() string { return env.GetEnv("PORT", "7123") }, false},
|
|
{"AT_ENDPOINT", func() string { return env.GetEnv("AT_ENDPOINT", "/ussd/africastalking") }, false},
|
|
{"DB_CONN", func() string { return env.GetEnv("DB_CONN", "") }, true},
|
|
{"DB_TIMEZONE", func() string { return env.GetEnv("DB_TIMEZONE", "") }, false},
|
|
{"DB_SCHEMA", func() string { return env.GetEnv("DB_SCHEMA", "") }, false},
|
|
{"CUSTODIAL_URL_BASE", func() string { return env.GetEnv("CUSTODIAL_URL_BASE", "") }, false},
|
|
{"BEARER_TOKEN", func() string { return env.GetEnv("BEARER_TOKEN", "") }, true},
|
|
{"DATA_URL_BASE", func() string { return env.GetEnv("DATA_URL_BASE", "") }, false},
|
|
{"DEFAULT_LANGUAGE", func() string { return env.GetEnv("DEFAULT_LANGUAGE", "eng") }, false},
|
|
{"LANGUAGES", func() string { return env.GetEnv("LANGUAGES", "eng") }, false},
|
|
}
|
|
}
|
|
|
|
func (r *Reporter) AddValue(key string, valueFn func() string, sensitive bool) {
|
|
r.values = append(r.values, ConfigValue{
|
|
Key: key,
|
|
ValueFunc: valueFn,
|
|
Sensitive: sensitive,
|
|
})
|
|
}
|
|
|
|
// Report outputs all configuration values at the specified log level
|
|
func (r *Reporter) Report(level string) {
|
|
r.logger.Debugf("Configuration Report:")
|
|
for _, cv := range r.values {
|
|
value := cv.ValueFunc()
|
|
if cv.Sensitive {
|
|
value = "****"
|
|
}
|
|
switch level {
|
|
case "DEBUG":
|
|
r.logger.Debugf("%s: %s", cv.Key, value)
|
|
case "INFO":
|
|
r.logger.Infof("%s: %s", cv.Key, value)
|
|
case "WARN":
|
|
r.logger.Warnf("%s: %s", cv.Key, value)
|
|
case "ERROR":
|
|
r.logger.Errorf("%s: %s", cv.Key, value)
|
|
default:
|
|
r.logger.Infof("%s: %s", cv.Key, value)
|
|
}
|
|
}
|
|
} |