Log information to console if using custom react bundle

This commit is contained in:
Aurora Lahtela 2023-03-04 09:43:51 +02:00
parent 0fb493f236
commit 13cc314238
2 changed files with 50 additions and 19 deletions

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.delivery.web;
import com.djrapitops.plan.delivery.web.resource.WebResource;
import com.djrapitops.plan.delivery.webserver.Addresses;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.ResourceSettings;
import com.djrapitops.plan.settings.locale.Locale;
@ -26,6 +27,7 @@ import com.djrapitops.plan.storage.file.Resource;
import com.djrapitops.plan.utilities.dev.Untrusted;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import dagger.Lazy;
import net.playeranalytics.plugin.server.PluginLogger;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.TextStringBuilder;
@ -56,18 +58,21 @@ public class ResourceSvc implements ResourceService {
private final Locale locale;
private final PluginLogger logger;
private final ErrorLogger errorLogger;
private final Lazy<Addresses> addresses;
@Inject
public ResourceSvc(
PublicHtmlFiles publicHtmlFiles,
PlanConfig config,
Locale locale,
Lazy<Addresses> addresses,
PluginLogger logger,
ErrorLogger errorLogger
) {
this.publicHtmlFiles = publicHtmlFiles;
this.resourceSettings = config.getResourceSettings();
this.locale = locale;
this.addresses = addresses;
this.logger = logger;
this.errorLogger = errorLogger;
this.snippets = new HashSet<>();
@ -139,7 +144,9 @@ public class ResourceSvc implements ResourceService {
html = StringUtils.replaceOnce(html, "</head>", toHead.append("</head>").toString());
}
StringBuilder toBody = byPosition.get(Position.PRE_MAIN_SCRIPT);
html = StringUtils.replaceOnce(html, "<script>/* This script tag will be replaced with scripts */</script>", toBody.toString());
if (toBody != null) {
html = StringUtils.replaceOnce(html, "<script></script>", toBody.toString());
}
return html;
} else {
return applyLegacy(byPosition, html);
@ -207,8 +214,8 @@ public class ResourceSvc implements ResourceService {
public void addScriptsToResource(String pluginName, String fileName, Position position, String... jsSources) {
checkParams(pluginName, fileName, position, jsSources);
String snippet = new TextStringBuilder("<script src=\"")
.appendWithSeparators(jsSources, "\"></script><script src=\"")
String snippet = new TextStringBuilder("<script src=\"").append(getBasePath())
.appendWithSeparators(jsSources, "\"></script><script src=\"" + getBasePath())
.append("\"></script>").build();
snippets.add(new Snippet(pluginName, fileName, position, snippet));
if (!"Plan".equals(pluginName)) {
@ -238,8 +245,8 @@ public class ResourceSvc implements ResourceService {
public void addStylesToResource(String pluginName, String fileName, Position position, String... cssSources) {
checkParams(pluginName, fileName, position, cssSources);
String snippet = new TextStringBuilder("<link href=\"")
.appendWithSeparators(cssSources, "\" rel=\"stylesheet\"></link><link href=\"")
String snippet = new TextStringBuilder("<link href=\"").append(getBasePath())
.appendWithSeparators(cssSources, "\" rel=\"stylesheet\"></link><link href=\"" + getBasePath())
.append("\" rel=\"stylesheet\">").build();
snippets.add(new Snippet(pluginName, fileName, position, snippet));
if (!"Plan".equals(pluginName)) {
@ -247,6 +254,12 @@ public class ResourceSvc implements ResourceService {
}
}
private String getBasePath() {
String address = addresses.get().getMainAddress()
.orElseGet(addresses.get()::getFallbackLocalhostAddress);
return addresses.get().getBasePath(address);
}
private static class Snippet {
private final String pluginName;
private final String fileName;

View File

@ -20,6 +20,10 @@ import com.djrapitops.plan.SubSystem;
import com.djrapitops.plan.delivery.web.ResourceService;
import com.djrapitops.plan.delivery.webserver.auth.ActiveCookieStore;
import com.djrapitops.plan.delivery.webserver.http.WebServer;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.PluginSettings;
import com.djrapitops.plan.storage.file.PublicHtmlFiles;
import net.playeranalytics.plugin.server.PluginLogger;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -32,37 +36,51 @@ import javax.inject.Singleton;
@Singleton
public class WebServerSystem implements SubSystem {
private final PlanConfig config;
private final Addresses addresses;
private final ActiveCookieStore activeCookieStore;
private final PublicHtmlFiles publicHtmlFiles;
private final WebServer webServer;
private final PluginLogger logger;
@Inject
public WebServerSystem(
PlanConfig config,
Addresses addresses,
ActiveCookieStore activeCookieStore,
WebServer webServer
) {
PublicHtmlFiles publicHtmlFiles,
WebServer webServer,
PluginLogger logger) {
this.config = config;
this.addresses = addresses;
this.activeCookieStore = activeCookieStore;
this.publicHtmlFiles = publicHtmlFiles;
this.webServer = webServer;
this.logger = logger;
}
@Override
public void enable() {
activeCookieStore.enable();
webServer.enable();
if (!webServer.isAuthRequired()) {
ResourceService.getInstance().addStylesToResource("Plan", "error.html", ResourceService.Position.PRE_CONTENT, "./css/noauth.css");
ResourceService.getInstance().addStylesToResource("Plan", "server.html", ResourceService.Position.PRE_CONTENT, "../css/noauth.css");
ResourceService.getInstance().addStylesToResource("Plan", "player.html", ResourceService.Position.PRE_CONTENT, "../css/noauth.css");
ResourceService.getInstance().addStylesToResource("Plan", "players.html", ResourceService.Position.PRE_CONTENT, "./css/noauth.css");
ResourceService.getInstance().addStylesToResource("Plan", "network.html", ResourceService.Position.PRE_CONTENT, "./css/noauth.css");
ResourceService.getInstance().addStylesToResource("Plan", "query.html", ResourceService.Position.PRE_CONTENT, "./css/noauth.css");
}
if (webServer.isEnabled()) {
ResourceService.getInstance().addStylesToResource("Plan", "server.html", ResourceService.Position.PRE_CONTENT, "../css/querybutton.css");
ResourceService.getInstance().addStylesToResource("Plan", "players.html", ResourceService.Position.PRE_CONTENT, "./css/querybutton.css");
ResourceService.getInstance().addStylesToResource("Plan", "network.html", ResourceService.Position.PRE_CONTENT, "./css/querybutton.css");
if (config.isTrue(PluginSettings.LEGACY_FRONTEND)) {
if (!webServer.isAuthRequired()) {
ResourceService.getInstance().addStylesToResource("Plan", "error.html", ResourceService.Position.PRE_CONTENT, "./css/noauth.css");
ResourceService.getInstance().addStylesToResource("Plan", "server.html", ResourceService.Position.PRE_CONTENT, "../css/noauth.css");
ResourceService.getInstance().addStylesToResource("Plan", "player.html", ResourceService.Position.PRE_CONTENT, "../css/noauth.css");
ResourceService.getInstance().addStylesToResource("Plan", "players.html", ResourceService.Position.PRE_CONTENT, "./css/noauth.css");
ResourceService.getInstance().addStylesToResource("Plan", "network.html", ResourceService.Position.PRE_CONTENT, "./css/noauth.css");
ResourceService.getInstance().addStylesToResource("Plan", "query.html", ResourceService.Position.PRE_CONTENT, "./css/noauth.css");
}
if (webServer.isEnabled()) {
ResourceService.getInstance().addStylesToResource("Plan", "server.html", ResourceService.Position.PRE_CONTENT, "../css/querybutton.css");
ResourceService.getInstance().addStylesToResource("Plan", "players.html", ResourceService.Position.PRE_CONTENT, "./css/querybutton.css");
ResourceService.getInstance().addStylesToResource("Plan", "network.html", ResourceService.Position.PRE_CONTENT, "./css/querybutton.css");
}
} else {
if (publicHtmlFiles.findPublicHtmlResource("index.html").isPresent()) {
logger.info("Found index.html in public_html, using a custom React bundle!");
}
}
}