Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit854a10e

Browse files
authored
Merge pull request#1766 from lowcoder-org/fix/add_default_variables
Add default values for missing environment variables
2 parents713f1f7 +400c53a commit854a10e

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
packageorg.lowcoder.domain.serversetting.service;
22

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

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

1418

19+
@RequiredArgsConstructor
1520
@Slf4j
1621
@Service
1722
publicclassServerSettingServiceImplimplementsServerSettingService {
23+
24+
privatefinalEnvironmentenvironment;
1825
privatefinalServerSettingRepositoryrepository;
26+
1927
privatefinalList<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-
publicServerSettingServiceImpl(ServerSettingRepositoryrepository) {
38-
this.repository =repository;
39-
}
40-
4144
@Override
4245
publicMono<Map<String,String>>getServerSettingsMap() {
4346
returnrepository.findAll().collectMap(ServerSetting::getKey,ServerSetting::getValue);
4447
}
4548

4649
@PostConstruct
4750
publicvoidsaveEnvironmentVariables() {
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 =newTreeMap<>(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 =newTreeMap<>(defaults);
59+
merged.keySet().removeAll(envVariables.keySet());
60+
merged.putAll(envVariables);
61+
62+
Flux.fromIterable(merged.keySet())
5163
.map(key -> {
5264
Stringvalue =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+
privateMap<String,String>getEnvironmentVariablesDefaults() {
79+
Map<String,String>defaults =newHashMap<>();
80+
81+
MutablePropertySourcespropertySources = ((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+
StringrawValue =Objects.toString(propertySource.getProperty(name),"");
90+
if (rawValue !=null &&StringUtils.contains(rawValue,"${LOWCODER_")) {
91+
StringdefaultValue =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+
returndefaults;
101+
}
64102
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp