Skip to content
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

Configurable home directory #315

Merged
merged 3 commits into from
Oct 31, 2024
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
25 changes: 24 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,30 @@ cms-server/modules/seo-module
cms-server/modules/forms-module
cms-server/modules/markedj-module
cms-server/cms.pid
/cms-server/hosts/features/data/
/distribution/temp/
/distribution/build/
/distribution/dist/
/cms-server/hosts/features/data/
test-server/hosts/marx-software
test-server/hosts/demo/modules_data/search-module/index
test-server/hosts/demo/modules_data/ui-module
test-server/hosts/demo/temp/
test-server/hosts/features/modules_data/
test-server/hosts/features/temp
test-server/hosts/features_de/modules_data/
test-server/hosts/features_de/temp/
test-server/hosts/theme-demo/modules_data/search-module/index
test-server/hosts/theme-demo/modules_data/ui-module
test-server/modules/flexmark-module
test-server/modules/markedjs-module
test-server/modules/pebble-module
test-server/modules/thymeleaf-module
test-server/modules/freemarker-module
test-server/modules/ui-module
test-server/modules/pug-module
test-server/modules/search-module
test-server/modules/seo-module
test-server/modules/forms-module
test-server/modules/markedj-module
test-server/cms.pid
test-server/hosts/features/data/
2 changes: 2 additions & 0 deletions cms-api/src/main/java/com/condation/cms/api/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public static class Folders {
public static final String ASSETS = "assets/";
public static final String EXTENSIONS = "extensions/";
public static final String MODULES = "modules/";
public static final String HOSTS = "hosts/";
public static final String THEMES = "themes/";
}

public static class NodeType {
Expand Down
44 changes: 44 additions & 0 deletions cms-api/src/main/java/com/condation/cms/api/utils/ServerUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.condation.cms.api.utils;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 - 2024 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 java.nio.file.Path;

public final class ServerUtil {

private ServerUtil(){}

public static Path getHome () {
if (System.getenv().containsKey("CMS_HOME")) {
return Path.of(System.getenv("CMS_HOME"));
}
if (System.getProperties().containsKey("cms.home")) {
return Path.of(System.getProperties().getProperty("cms.home"));
}
return Path.of(".");
}

public static Path getPath (String path) {
return getHome().resolve(path);
}
}
22 changes: 22 additions & 0 deletions cms-api/src/main/java/com/condation/cms/api/utils/SiteUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import com.condation.cms.api.SiteProperties;
import com.condation.cms.api.theme.Theme;

/**
*
Expand All @@ -38,4 +43,21 @@ public static boolean isSite(Path check) {
return Files.exists(check.resolve("site.yaml"))
|| Files.exists(check.resolve("site.toml"));
}

public static List<String> getActiveModules(SiteProperties siteProperties, Theme theme) {
List<String> activeModules = new ArrayList<>();
activeModules.addAll(siteProperties.activeModules());
if (!theme.empty()) {
activeModules.addAll(theme.properties().activeModules());

if (theme.getParentTheme() != null) {
activeModules.addAll(theme.getParentTheme().properties().activeModules());
}
}
return activeModules;
}

public static String getRequiredTheme(SiteProperties siteProperties, Theme theme) {
return siteProperties.theme();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.condation.cms.core.configuration;

import com.condation.cms.api.Constants;
/*-
* #%L
* tests
Expand All @@ -25,6 +26,7 @@
import com.condation.cms.core.configuration.configs.SimpleConfiguration;
import com.condation.cms.api.eventbus.EventBus;
import com.condation.cms.api.scheduler.CronJobScheduler;
import com.condation.cms.api.utils.ServerUtil;
import com.condation.cms.core.configuration.configs.MediaConfiguration;
import com.condation.cms.core.configuration.configs.TaxonomyConfiguration;
import com.condation.cms.core.configuration.reload.CronReload;
Expand Down Expand Up @@ -93,11 +95,12 @@ public static SimpleConfiguration themeConfiguration(String id, String themePath
return themeConfiguration(id, null, themePath);
}
private static SimpleConfiguration themeConfiguration(String id, EventBus eventBus, String theme) throws IOException {
var themeBase = ServerUtil.getPath(Constants.Folders.THEMES);
return SimpleConfiguration.builder(eventBus)
.id(id)
.reloadStrategy(new NoReload())
.addSource(YamlConfigSource.build(Path.of("themes/%s/theme.yaml".formatted(theme))))
.addSource(TomlConfigSource.build(Path.of("themes/%s/theme.toml".formatted(theme))))
.addSource(YamlConfigSource.build(themeBase.resolve("%s/theme.yaml".formatted(theme))))
.addSource(TomlConfigSource.build(themeBase.resolve("%s/theme.toml".formatted(theme))))
.build();
}

Expand All @@ -108,18 +111,19 @@ private static SimpleConfiguration serverConfiguration(EventBus eventBus) throws
return SimpleConfiguration.builder(eventBus)
.id("server")
.reloadStrategy(new NoReload())
.addSource(YamlConfigSource.build(Path.of("server.yaml")))
.addSource(TomlConfigSource.build(Path.of("server.toml")))
.addSource(YamlConfigSource.build(ServerUtil.getPath("server.yaml")))
.addSource(TomlConfigSource.build(ServerUtil.getPath("server.toml")))
.build();
}

private static MediaConfiguration mediaConfiguration(EventBus eventBus, Path hostBase, List<String> themes) throws IOException {
var themeBase = ServerUtil.getPath(Constants.Folders.THEMES);
List<ConfigSource> themeSources = new ArrayList<>();
for (String theme : themes) {
themeSources.add(
YamlConfigSource.build(Path.of("themes/%s/config/media.yaml".formatted(theme))));
YamlConfigSource.build(themeBase.resolve("%s/config/media.yaml".formatted(theme))));
themeSources.add(
TomlConfigSource.build(Path.of("themes/%s/config/media.toml".formatted(theme))));
TomlConfigSource.build(themeBase.resolve("%s/config/media.toml".formatted(theme))));
};

themeSources.add(YamlConfigSource.build(hostBase.resolve("config/media.yaml")));
Expand Down Expand Up @@ -165,6 +169,7 @@ private static TaxonomyConfiguration taxonomyConfiguration(EventBus eventBus, Pa
return TaxonomyConfiguration.builder(eventBus)
.id("taxonomy")
.reloadStrategy(reloadStrategy)
.hostBase(hostBase)
.addSource(YamlConfigSource.build(hostBase.resolve("config/taxonomy.yaml")))
.addSource(TomlConfigSource.build(hostBase.resolve("config/taxonomy.toml")))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class TaxonomyConfiguration extends AbstractConfiguration implements ICon
private final ReloadStrategy reloadStrategy;
private final EventBus eventBus;
private final String id;
private final Path hostBase;

private ConcurrentMap<String, Taxonomy> taxonomies = new ConcurrentHashMap<>();

Expand All @@ -64,6 +65,7 @@ public TaxonomyConfiguration(Builder builder) {
this.reloadStrategy = builder.reloadStrategy;
this.eventBus = builder.eventBus;
this.id = builder.id;
this.hostBase = builder.hostBase;
reloadStrategy.register(this);

reload();
Expand Down Expand Up @@ -109,8 +111,8 @@ private void loadValues(Taxonomy taxonomy) {
var tomlFile = "configs/taxonomy.%s.toml".formatted(taxonomy.getSlug());

var valueSrc = List.of(
YamlConfigSource.build(Path.of(yamlFile)),
TomlConfigSource.build(Path.of(tomlFile))
YamlConfigSource.build(hostBase.resolve(yamlFile)),
TomlConfigSource.build(hostBase.resolve(tomlFile))
);

var values = valueSrc.stream()
Expand Down Expand Up @@ -138,11 +140,17 @@ public static class Builder {
private ReloadStrategy reloadStrategy = new NoReload();
private String id = UUID.randomUUID().toString();
private final EventBus eventBus;
private Path hostBase;

public Builder (EventBus eventbus) {
this.eventBus = eventbus;
}

public Builder hostBase (Path hostBase) {
this.hostBase = hostBase;
return this;
}

public Builder id (String uniqueId) {
this.id = uniqueId;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.condation.cms.api.IPCProperties;
import com.condation.cms.api.PerformanceProperties;
import com.condation.cms.api.ServerProperties;
import com.condation.cms.api.utils.ServerUtil;
import com.condation.cms.core.configuration.configs.SimpleConfiguration;
import java.nio.file.Path;

Expand Down Expand Up @@ -59,7 +60,7 @@ public APMProperties apm() {

@Override
public Path getThemesFolder() {
return Path.of("themes/");
return ServerUtil.getPath(Constants.Folders.THEMES);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.condation.cms.core.theme;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;

/*-
* #%L
* cms-server
Expand All @@ -25,22 +29,15 @@
import com.condation.cms.api.ServerProperties;
import com.condation.cms.api.SiteProperties;
import com.condation.cms.api.ThemeProperties;
import com.condation.cms.core.messages.EmptyMessageSource;
import com.condation.cms.api.messages.MessageSource;
import com.condation.cms.core.messages.ThemeMessageSource;
import com.condation.cms.api.theme.Theme;
import com.condation.cms.core.configuration.ConfigManagement;
import com.condation.cms.core.configuration.ConfigurationFactory;
import com.condation.cms.core.configuration.properties.ExtendedThemeProperties;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import com.condation.cms.core.messages.EmptyMessageSource;
import com.condation.cms.core.messages.ThemeMessageSource;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class TaxonomyConfigurationTest {
public void setup() throws IOException, SchedulerException {
configuration = TaxonomyConfiguration.builder(eventBus)
.id("taxonomy-config")
.hostBase(Path.of("."))
.addSource(YamlConfigSource.build(Path.of("configs/taxonomy.yaml")))
.addSource(TomlConfigSource.build(Path.of("configs/taxonomy.toml")))
.build();
Expand Down
4 changes: 4 additions & 0 deletions cms-git/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<artifactId>cms-git</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.condation.cms</groupId>
<artifactId>cms-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.condation.cms.git.tasks;

import com.condation.cms.api.utils.ServerUtil;

/*-
* #%L
* cms-git
Expand Down Expand Up @@ -47,7 +49,7 @@ public class CloneTask implements Task<Boolean> {
@Override
public Boolean call() throws Exception {

Path targetFolder = Path.of(repo.getFolder());
Path targetFolder = ServerUtil.getPath(repo.getFolder());
if (Files.exists(targetFolder)) {
log.trace("repository already cloned");
return Boolean.TRUE;
Expand All @@ -62,7 +64,7 @@ public Boolean call() throws Exception {
try {
result = Git.cloneRepository()
.setURI(repo.getUri())
.setDirectory(new File(repo.getFolder()))
.setDirectory(targetFolder.toFile())
.setBranchesToClone(Arrays.asList("refs/heads/" + repo.getBranch()))
.setBranch("refs/heads/" + repo.getBranch())
.setCredentialsProvider(credentialProvider)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.condation.cms.git.tasks;

import com.condation.cms.api.utils.ServerUtil;

/*-
* #%L
* cms-git
Expand Down Expand Up @@ -45,7 +47,7 @@ public class FetchTask implements Task<Boolean> {
@Override
public Boolean call() throws Exception {

Path targetFolder = Path.of(repo.getFolder());
Path targetFolder = ServerUtil.getPath(repo.getFolder());
if (!Files.exists(targetFolder)) {
log.trace("target folder does not exists");
return Boolean.FALSE;
Expand All @@ -56,7 +58,7 @@ public Boolean call() throws Exception {
repo.getCredentials().getPassword()
);

Git git_repo = Git.open(new File(repo.getFolder()));
Git git_repo = Git.open(targetFolder.toFile());
try {

var result = git_repo.fetch()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.condation.cms.git.tasks;

import com.condation.cms.api.utils.ServerUtil;

/*-
* #%L
* cms-git
Expand Down Expand Up @@ -45,7 +47,7 @@ public class MergeTask implements Task<Boolean> {
@Override
public Boolean call() throws Exception {

Path targetFolder = Path.of(repo.getFolder());
Path targetFolder = ServerUtil.getPath(repo.getFolder());
if (!Files.exists(targetFolder)) {
log.trace("target folder does not exists");
return Boolean.FALSE;
Expand All @@ -56,7 +58,7 @@ public Boolean call() throws Exception {
repo.getCredentials().getPassword()
);

Git git_repo = Git.open(new File(repo.getFolder()));
Git git_repo = Git.open(targetFolder.toFile());
try {

var result = git_repo.merge()
Expand Down
Loading