@@ -25,13 +25,15 @@ var ENV *structure.ENV = &structure.ENV{
2525 INSECURE : false ,
2626}
2727
28- var defaultsConf = configutils .New ()
29- var userConf = configutils .New ()
30- var tokenConf = configutils .New ()
28+ var defaultsConf * configutils.Config
29+ var userConf * configutils.Config
30+ var tokenConf * configutils.Config
3131
32- var config = configutils .New ()
32+ var mainConf * configutils.Config
3333
3434func Load () {
35+ Clear ()
36+
3537 InitReload ()
3638
3739 LoadDefaults ()
@@ -42,39 +44,90 @@ func Load() {
4244
4345 userConf .LoadEnv ()
4446
45- config .MergeLayers (defaultsConf .Layer , userConf .Layer )
47+ NormalizeConfig (defaultsConf )
48+ NormalizeConfig (userConf )
49+
50+ NormalizeTokens ()
51+
52+ mainConf .MergeLayers (defaultsConf .Layer , userConf .Layer )
4653
47- config .NormalizeKeys ()
48- config .TemplateConfig ()
54+ mainConf .TemplateConfig ()
4955
5056 InitTokens ()
5157
5258 InitEnv ()
5359
5460 log .Info ("Finished Loading Configuration" )
5561
56- log .Dev ("Loaded Config:\n " + jsonutils .ToJson (config .Layer .All ()))
62+ log .Dev ("Loaded Config:\n " + jsonutils .ToJson (mainConf .Layer .All ()))
5763 log .Dev ("Loaded Token Configs:\n " + jsonutils .ToJson (tokenConf .Layer .All ()))
5864}
5965
66+ func Clear () {
67+ defaultsConf = configutils .New ()
68+ userConf = configutils .New ()
69+ tokenConf = configutils .New ()
70+ mainConf = configutils .New ()
71+ }
72+
73+ func LowercaseKeys (config * configutils.Config ) {
74+ data := map [string ]any {}
75+
76+ for _ , key := range config .Layer .Keys () {
77+ lower := strings .ToLower (key )
78+
79+ data [lower ] = config .Layer .Get (key )
80+ }
81+
82+ config .Layer .Delete ("" )
83+ config .Load (data , "" )
84+ }
85+
86+ func NormalizeConfig (config * configutils.Config ) {
87+ Normalize (config , "settings" , & structure.SETTINGS {})
88+ }
89+
90+ func Normalize (config * configutils.Config , path string , structure any ) {
91+ data := config .Layer .Get (path )
92+ old , ok := data .(map [string ]any )
93+
94+ if ! ok {
95+ log .Warn ("Could not load `" + path + "`" )
96+ return
97+ }
98+
99+ // Create temporary config
100+ tmpConf := configutils .New ()
101+ tmpConf .Load (old , "" )
102+
103+ // Apply transforms to the new config
104+ tmpConf .ApplyTransformFuncs (structure , "" , transformFuncs )
105+
106+ // Lowercase actual config
107+ LowercaseKeys (config )
108+
109+ // Load temporary config back into paths
110+ config .Layer .Delete (path )
111+
112+ config .Load (tmpConf .Layer .All (), path )
113+ }
114+
60115func InitReload () {
61116 defaultsConf .OnLoad (Load )
62117 userConf .OnLoad (Load )
63118 tokenConf .OnLoad (Load )
64119}
65120
66121func InitEnv () {
67- ENV .PORT = strconv .Itoa (config .Layer .Int ("service.port" ))
122+ ENV .PORT = strconv .Itoa (mainConf .Layer .Int ("service.port" ))
68123
69- ENV .LOG_LEVEL = strings .ToLower (config .Layer .String ("loglevel" ))
124+ ENV .LOG_LEVEL = strings .ToLower (mainConf .Layer .String ("loglevel" ))
70125
71- ENV .API_URL = config .Layer .String ("api.url" )
126+ ENV .API_URL = mainConf .Layer .String ("api.url" )
72127
73128 var settings structure.SETTINGS
74129
75- config .TransformChildren ("settings.message.variables" , transformVariables )
76-
77- config .Layer .Unmarshal ("settings" , & settings )
130+ mainConf .Layer .Unmarshal ("settings" , & settings )
78131
79132 ENV .SETTINGS ["*" ] = & settings
80133}
@@ -101,8 +154,4 @@ func LoadConfig() {
101154
102155 log .Error ("Could not Load Config " , ENV .CONFIG_PATH , ": " , err .Error ())
103156 }
104- }
105-
106- func transformVariables (key string , value any ) (string , any ) {
107- return strings .ToUpper (key ), value
108- }
157+ }
0 commit comments