Skip to content

Commit 400c53a

Browse files
committed
fix: add default values for environment variables
1 parent 8f2b188 commit 400c53a

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

server/api-service/lowcoder-domain/src/main/java/org/lowcoder/domain/serversetting/service/ServerSettingServiceImpl.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
package org.lowcoder.domain.serversetting.service;
22

3+
import lombok.RequiredArgsConstructor;
34
import lombok.extern.slf4j.Slf4j;
5+
import org.apache.commons.lang3.StringUtils;
46
import org.lowcoder.domain.serversetting.model.ServerSetting;
57
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.core.env.*;
69
import org.springframework.stereotype.Service;
710
import reactor.core.publisher.Flux;
811
import reactor.core.publisher.Mono;
912

1013
import javax.annotation.PostConstruct;
11-
import java.util.List;
12-
import java.util.Map;
14+
import java.util.*;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.StreamSupport;
1317

1418

19+
@RequiredArgsConstructor
1520
@Slf4j
1621
@Service
1722
public class ServerSettingServiceImpl implements ServerSettingService {
23+
24+
private final Environment environment;
1825
private final ServerSettingRepository repository;
26+
1927
private final List<String> EXCLUDED_KEYS = List.of("LOWCODER_MONGODB_EXPOSED",
2028
"LOWCODER_PUID",
2129
"LOWCODER_PGID",
@@ -33,21 +41,25 @@ public class ServerSettingServiceImpl implements ServerSettingService {
3341
"LOWCODER_NODE_SERVICE_SECRET",
3442
"LOWCODER_NODE_SERVICE_SECRET_SALT");
3543

36-
@Autowired
37-
public ServerSettingServiceImpl(ServerSettingRepository repository) {
38-
this.repository = repository;
39-
}
40-
4144
@Override
4245
public Mono<Map<String, String>> getServerSettingsMap() {
4346
return repository.findAll().collectMap(ServerSetting::getKey, ServerSetting::getValue);
4447
}
4548

4649
@PostConstruct
4750
public void saveEnvironmentVariables() {
48-
Map<String, String> envVariables = System.getenv();
49-
Flux.fromIterable(envVariables.keySet())
50-
.filter(key -> key.startsWith("LOWCODER_"))
51+
52+
Map<String, String> defaults = getEnvironmentVariablesDefaults();
53+
54+
Map<String, String> envVariables = new TreeMap<>(System.getenv().entrySet().stream()
55+
.filter(entry -> StringUtils.startsWith(entry.getKey(), "LOWCODER_"))
56+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
57+
58+
Map<String, String> merged = new TreeMap<>(defaults);
59+
merged.keySet().removeAll(envVariables.keySet());
60+
merged.putAll(envVariables);
61+
62+
Flux.fromIterable(merged.keySet())
5163
.map(key -> {
5264
String value = envVariables.getOrDefault(key, "");
5365
if(EXCLUDED_KEYS.contains(key)) {
@@ -61,4 +73,30 @@ public void saveEnvironmentVariables() {
6173
.flatMap(repository::save)
6274
.subscribe();
6375
}
76+
77+
78+
private Map<String, String> getEnvironmentVariablesDefaults() {
79+
Map<String, String> defaults = new HashMap<>();
80+
81+
MutablePropertySources propertySources = ((AbstractEnvironment) environment).getPropertySources();
82+
StreamSupport.stream(propertySources.spliterator(), false)
83+
.filter(EnumerablePropertySource.class::isInstance)
84+
.map(EnumerablePropertySource.class::cast)
85+
.forEach(propertySource -> {
86+
String[] names = propertySource.getPropertyNames();
87+
if (names.length > 0) {
88+
Arrays.stream(names).forEach(name -> {
89+
String rawValue = Objects.toString(propertySource.getProperty(name), "");
90+
if (rawValue != null && StringUtils.contains(rawValue, "${LOWCODER_")) {
91+
String defaultValue = StringUtils.substringBetween(rawValue, "${", "}");
92+
String[] keyValue = StringUtils.split(defaultValue, ":");
93+
if (keyValue.length == 2 && !defaults.containsKey(keyValue[0])) {
94+
defaults.put(keyValue[0], keyValue[1]);
95+
}
96+
}
97+
});
98+
}
99+
});
100+
return defaults;
101+
}
64102
}

0 commit comments

Comments
 (0)