Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions cms-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<artifactId>jtoml-serializer-gson</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.condation.cms.core.configuration.configs.TaxonomyConfiguration;
import com.condation.cms.core.configuration.reload.CronReload;
import com.condation.cms.core.configuration.reload.NoReload;
import com.condation.cms.core.configuration.source.EnvConfigSource;
import com.condation.cms.core.configuration.source.TomlConfigSource;
import com.condation.cms.core.configuration.source.YamlConfigSource;
import java.io.IOException;
Expand Down Expand Up @@ -110,6 +111,7 @@ private static SimpleConfiguration serverConfiguration(EventBus eventBus) throws
return SimpleConfiguration.builder(eventBus)
.id("server")
.reloadStrategy(new NoReload())
.addSource(new EnvConfigSource())
.addSource(TomlConfigSource.build(ServerUtil.getPath("server.toml")))
.addSource(YamlConfigSource.build(ServerUtil.getPath("server.yaml")))
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.condation.cms.core.configuration.source;

/*-
* #%L
* cms-core
* %%
* Copyright (C) 2023 - 2025 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.condation.cms.api.utils.MapUtil;
import com.condation.cms.api.utils.ServerUtil;
import com.condation.cms.core.configuration.ConfigSource;
import com.condation.cms.core.utils.EnvUtil;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class EnvConfigSource implements ConfigSource {

private final Map<String, Object> envVariables;

public EnvConfigSource () {
this(ServerUtil.getHome().toAbsolutePath().toString());
}

public EnvConfigSource(String path) {
this.envVariables = EnvUtil.load(path);
}

@Override
public boolean reload() {
// Environment variables are not expected to change during runtime
return false;
}

@Override
public boolean exists() {
// Environment variables are always considered to exist
return true;
}

@Override
public String getString(String field) {
return (String) MapUtil.getValue(envVariables, field);
}

@Override
public Object get(String field) {
return MapUtil.getValue(envVariables, field);
}

@Override
public Map<String, Object> getMap(String field) {
return MapUtil.getValue(envVariables, field, Collections.emptyMap());
}

@Override
public List<Object> getList(String field) {
return MapUtil.getValue(envVariables, field, Collections.emptyList());
}
}
84 changes: 84 additions & 0 deletions cms-core/src/main/java/com/condation/cms/core/utils/EnvUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.condation.cms.core.utils;

/*-
* #%L
* cms-core
* %%
* Copyright (C) 2023 - 2025 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import io.github.cdimascio.dotenv.Dotenv;
import java.util.HashMap;
import java.util.Map;

public class EnvUtil {

public static Map<String, Object> load(String path) {
Map<String, Object> envMap = new HashMap<>();

// Load .env file if it exists
Dotenv dotenv = Dotenv.configure().directory(path).ignoreIfMissing().load();
dotenv.entries().forEach(entry -> {
String key = entry.getKey();
String value = entry.getValue();
addValueToMap(envMap, key, value);
});

// Load system environment variables, overriding .env variables
System.getenv().forEach((key, value) -> {
addValueToMap(envMap, key, value);
});

// Load system properties, overriding system environment variables
System.getProperties().forEach((key, value) -> {
addValueToMap(envMap, (String) key, (String) value);
});

return envMap;
}

private static void addValueToMap(Map<String, Object> map, String key, String value) {
if (key == null || key.trim().isEmpty()) {
return;
}
String[] parts = key.split("_");
if (parts.length == 0) {
return;
}

Map<String, Object> currentMap = map;
for (int i = 0; i < parts.length - 1; i++) {
String part = parts[i].toLowerCase();
if (part.isEmpty()) {
continue;
}
Object node = currentMap.get(part);
if (node instanceof Map) {
currentMap = (Map<String, Object>) node;
} else if (node == null) {
Map<String, Object> newMap = new HashMap<>();
currentMap.put(part, newMap);
currentMap = newMap;
} else {
// A value is already present, cannot create a map here.
return;
}
}
currentMap.put(parts[parts.length - 1].toLowerCase(), value);
}
}
1 change: 1 addition & 0 deletions test-server/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UI_SECRET=super-secret-token
2 changes: 0 additions & 2 deletions test-server/server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ env = "dev"
port = 2020
ip = "127.0.0.1"

[ui]
secret = "super-secret-token"

[ipc]
port = 6868
Expand Down