Skip to content

Add default values for missing environment variables #1766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package org.lowcoder.domain.serversetting.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.lowcoder.domain.serversetting.model.ServerSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.*;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;


@RequiredArgsConstructor
@Slf4j
@Service
public class ServerSettingServiceImpl implements ServerSettingService {

private final Environment environment;
private final ServerSettingRepository repository;

private final List<String> EXCLUDED_KEYS = List.of("LOWCODER_MONGODB_EXPOSED",
"LOWCODER_PUID",
"LOWCODER_PGID",
Expand All @@ -33,21 +41,25 @@ public class ServerSettingServiceImpl implements ServerSettingService {
"LOWCODER_NODE_SERVICE_SECRET",
"LOWCODER_NODE_SERVICE_SECRET_SALT");

@Autowired
public ServerSettingServiceImpl(ServerSettingRepository repository) {
this.repository = repository;
}

@Override
public Mono<Map<String, String>> getServerSettingsMap() {
return repository.findAll().collectMap(ServerSetting::getKey, ServerSetting::getValue);
}

@PostConstruct
public void saveEnvironmentVariables() {
Map<String, String> envVariables = System.getenv();
Flux.fromIterable(envVariables.keySet())
.filter(key -> key.startsWith("LOWCODER_"))

Map<String, String> defaults = getEnvironmentVariablesDefaults();

Map<String, String> envVariables = new TreeMap<>(System.getenv().entrySet().stream()
.filter(entry -> StringUtils.startsWith(entry.getKey(), "LOWCODER_"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));

Map<String, String> merged = new TreeMap<>(defaults);
merged.keySet().removeAll(envVariables.keySet());
merged.putAll(envVariables);

Flux.fromIterable(merged.keySet())
.map(key -> {
String value = envVariables.getOrDefault(key, "");
if(EXCLUDED_KEYS.contains(key)) {
Expand All @@ -61,4 +73,30 @@ public void saveEnvironmentVariables() {
.flatMap(repository::save)
.subscribe();
}


private Map<String, String> getEnvironmentVariablesDefaults() {
Map<String, String> defaults = new HashMap<>();

MutablePropertySources propertySources = ((AbstractEnvironment) environment).getPropertySources();
StreamSupport.stream(propertySources.spliterator(), false)
.filter(EnumerablePropertySource.class::isInstance)
.map(EnumerablePropertySource.class::cast)
.forEach(propertySource -> {
String[] names = propertySource.getPropertyNames();
if (names.length > 0) {
Arrays.stream(names).forEach(name -> {
String rawValue = Objects.toString(propertySource.getProperty(name), "");
if (rawValue != null && StringUtils.contains(rawValue, "${LOWCODER_")) {
String defaultValue = StringUtils.substringBetween(rawValue, "${", "}");
String[] keyValue = StringUtils.split(defaultValue, ":");
if (keyValue.length == 2 && !defaults.containsKey(keyValue[0])) {
defaults.put(keyValue[0], keyValue[1]);
}
}
});
}
});
return defaults;
}
}
Loading