1
1
package org .lowcoder .domain .serversetting .service ;
2
2
3
+ import lombok .RequiredArgsConstructor ;
3
4
import lombok .extern .slf4j .Slf4j ;
5
+ import org .apache .commons .lang3 .StringUtils ;
4
6
import org .lowcoder .domain .serversetting .model .ServerSetting ;
5
7
import org .springframework .beans .factory .annotation .Autowired ;
8
+ import org .springframework .core .env .*;
6
9
import org .springframework .stereotype .Service ;
7
10
import reactor .core .publisher .Flux ;
8
11
import reactor .core .publisher .Mono ;
9
12
10
13
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 ;
13
17
14
18
19
+ @ RequiredArgsConstructor
15
20
@ Slf4j
16
21
@ Service
17
22
public class ServerSettingServiceImpl implements ServerSettingService {
23
+
24
+ private final Environment environment ;
18
25
private final ServerSettingRepository repository ;
26
+
19
27
private final List <String >EXCLUDED_KEYS =List .of ("LOWCODER_MONGODB_EXPOSED" ,
20
28
"LOWCODER_PUID" ,
21
29
"LOWCODER_PGID" ,
@@ -33,21 +41,25 @@ public class ServerSettingServiceImpl implements ServerSettingService {
33
41
"LOWCODER_NODE_SERVICE_SECRET" ,
34
42
"LOWCODER_NODE_SERVICE_SECRET_SALT" );
35
43
36
- @ Autowired
37
- public ServerSettingServiceImpl (ServerSettingRepository repository ) {
38
- this .repository =repository ;
39
- }
40
-
41
44
@ Override
42
45
public Mono <Map <String ,String >>getServerSettingsMap () {
43
46
return repository .findAll ().collectMap (ServerSetting ::getKey ,ServerSetting ::getValue );
44
47
}
45
48
46
49
@ PostConstruct
47
50
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 ())
51
63
.map (key -> {
52
64
String value =envVariables .getOrDefault (key ,"" );
53
65
if (EXCLUDED_KEYS .contains (key )) {
@@ -61,4 +73,30 @@ public void saveEnvironmentVariables() {
61
73
.flatMap (repository ::save )
62
74
.subscribe ();
63
75
}
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
+ }
64
102
}