Skip to content

Commit

Permalink
added environment variables to config tree
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzardo committed Sep 30, 2023
1 parent 4886645 commit d095c9a
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/main/java/com/wizzardo/http/framework/WebApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,42 @@ protected void loadDefaultDevelopmentConfiguration(Config config) {
}

protected void loadEnvironmentVariables(Config config) {
System.getenv().forEach(config::put);
outer:
for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
config.put(key, value);

String[] keys = key.split("_");
if (keys.length == 0)
continue;

Config subConfig = config;
int last = keys.length - 1;
int i = 0;
try {
for (; i < last; i++) {
String part = keys[i];
Optional<String> k = subConfig.keySet().stream().filter(it -> it.equalsIgnoreCase(part)).findFirst();
if (!k.isPresent())
continue outer;

subConfig = subConfig.config(k.get());
}

Optional<String> k = subConfig.keySet().stream().filter(it -> it.equalsIgnoreCase(keys[last])).findFirst();
if (!k.isPresent())
continue;
subConfig.put(k.get(), value);
} catch (ClassCastException e) {
String k = "";
for (int j = 0; j < i; j++) {
k += keys[j] + ".";
}
k += keys[i];
System.out.println("WARNING! cannot overwrite config value " + k + "=" + subConfig.get(keys[i]) + " with new config " + key + "=" + value);
}
}
}

protected void loadSystemProperties(Config config) {
Expand Down

0 comments on commit d095c9a

Please sign in to comment.