From 4594c5cd444497bcec7b7b00c17a79a709e0bcf3 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Sat, 13 Apr 2024 13:54:46 +0200 Subject: [PATCH] Allow embedded jetty to serve an external resources directory --- docker/README.md | 5 +++++ docker/pairgoth.properties.example | 2 ++ .../org/jeudego/pairgoth/web/BaseWebappManager.kt | 2 ++ .../org/jeudego/pairgoth/web/WebappManager.kt | 14 ++++++++++++++ .../org/jeudego/pairgoth/application/Pairgoth.kt | 8 ++++++-- 5 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 docker/README.md create mode 100644 docker/pairgoth.properties.example diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000..cf8a022 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,5 @@ +To run Pairgoth under docker: + +- compile Pairgoth and copy `application/target/pairgoth-engine.jar` to `docker/data/app` +- copy `pairgoth.properties.example` to `docker/data/app/pairgoth.properties` and adapt it to your needs +- launch `./run.sh` from within the `docker` directory diff --git a/docker/pairgoth.properties.example b/docker/pairgoth.properties.example new file mode 100644 index 0000000..a8cfdf1 --- /dev/null +++ b/docker/pairgoth.properties.example @@ -0,0 +1,2 @@ +auth = none + diff --git a/pairgoth-common/src/main/kotlin/org/jeudego/pairgoth/web/BaseWebappManager.kt b/pairgoth-common/src/main/kotlin/org/jeudego/pairgoth/web/BaseWebappManager.kt index 5e2c143..722617a 100644 --- a/pairgoth-common/src/main/kotlin/org/jeudego/pairgoth/web/BaseWebappManager.kt +++ b/pairgoth-common/src/main/kotlin/org/jeudego/pairgoth/web/BaseWebappManager.kt @@ -119,6 +119,8 @@ abstract class BaseWebappManager(val webappName: String, loggerName: String) : S // fail to correctly implement SSL... disableSSLCertificateChecks() + logger.info("webapp ${webappName} root filesystem path: ${context.getRealPath("")}") + } catch (ioe: IOException) { logger.error("webapp initialization error", ioe) } diff --git a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/web/WebappManager.kt b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/web/WebappManager.kt index bfe2174..774872c 100644 --- a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/web/WebappManager.kt +++ b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/web/WebappManager.kt @@ -6,6 +6,7 @@ import org.jeudego.pairgoth.ratings.RatingsManager import org.jeudego.pairgoth.util.Translator import org.slf4j.LoggerFactory import java.io.IOException +import java.nio.file.Paths import java.security.SecureRandom import java.security.cert.X509Certificate import java.util.* @@ -14,6 +15,8 @@ import javax.servlet.* import javax.servlet.annotation.WebListener import javax.servlet.http.HttpSessionEvent import javax.servlet.http.HttpSessionListener +import kotlin.io.path.createSymbolicLinkPointingTo +import kotlin.io.path.exists @WebListener class WebappManager : BaseWebappManager("View Webapp", "view") { @@ -22,6 +25,17 @@ class WebappManager : BaseWebappManager("View Webapp", "view") { override fun contextInitialized(sce: ServletContextEvent) { super.contextInitialized(sce) + // create a symlink for external resources access + properties.getProperty("cwd")?.let { cwd -> + val target = Paths.get(cwd).resolve("resources") + if (target.exists()) { + val source = Paths.get(context.getRealPath("/")).resolve("resources") + if (!source.exists()) { + source.createSymbolicLinkPointingTo(target) + } + } + } + // publish some properties to the webapp context; for easy access from the template context.setAttribute("env", properties.getProperty("env") ?: "dev") context.setAttribute("version", properties.getProperty("version") ?: "?") diff --git a/webserver/src/main/kotlin/org/jeudego/pairgoth/application/Pairgoth.kt b/webserver/src/main/kotlin/org/jeudego/pairgoth/application/Pairgoth.kt index b743b44..5ba8b9a 100644 --- a/webserver/src/main/kotlin/org/jeudego/pairgoth/application/Pairgoth.kt +++ b/webserver/src/main/kotlin/org/jeudego/pairgoth/application/Pairgoth.kt @@ -23,6 +23,7 @@ import java.net.URLDecoder import java.nio.charset.StandardCharsets import java.nio.file.Files import java.nio.file.Path +import java.nio.file.Paths import java.security.KeyFactory import java.security.KeyStore import java.security.cert.CertificateFactory @@ -109,10 +110,11 @@ private fun publishProperties() { } } // we want colorized output on linux - if (System.getProperty("os.name") == "Linux") - { + if (System.getProperty("os.name") == "Linux") { System.setProperty("org.eclipse.jetty.logging.appender.MESSAGE_ESCAPE", "false"); } + // remember the current working directory + System.setProperty("pairgoth.cwd", Paths.get("").toAbsolutePath().toString()) } private fun extractWarFiles() { @@ -219,6 +221,8 @@ private fun createContext(webapp: String, contextPath: String) = WebAppContext() context.contextPath = contextPath if (webapp == "api") { context.allowNullPathInfo = true + } else { + } }