From 5fdf3e89441b0deba06ed78ce14d7d4941421fc2 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Thu, 2 Nov 2023 11:05:40 +0100 Subject: [PATCH] Runtime properties renginering --- api-webapp/pom.xml | 29 ++++------- .../org/jeudego/pairgoth/oauth/OAuthHelper.kt | 2 +- .../org/jeudego/pairgoth/server/ApiServlet.kt | 2 +- .../jeudego/pairgoth/server/WebappManager.kt | 6 +-- .../WEB-INF/pairgoth.default.properties | 19 ++++++-- pom.xml | 48 +------------------ view-webapp/pom.xml | 35 ++++---------- .../org/jeudego/pairgoth/oauth/OAuthHelper.kt | 2 +- .../org/jeudego/pairgoth/view/ApiTool.kt | 4 +- .../org/jeudego/pairgoth/web/WebappManager.kt | 8 ++-- .../jeudego/pairgoth/application/Pairgoth.kt | 2 +- 11 files changed, 47 insertions(+), 110 deletions(-) diff --git a/api-webapp/pom.xml b/api-webapp/pom.xml index e0bd6cb..24e4db6 100644 --- a/api-webapp/pom.xml +++ b/api-webapp/pom.xml @@ -62,6 +62,15 @@ ${pairgoth.api.host} ${pairgoth.api.port} + + ${pairgoth.env} + ${pairgoth.api.external.url} + ${pairgoth.webapp.external.url} + ${pairgoth.store} + ${pairgoth.store} + ${pairgoth.logger.level} + ${pairgoth.logger.format} + ${pairgoth.api.context}/ @@ -87,26 +96,6 @@ - diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/oauth/OAuthHelper.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/oauth/OAuthHelper.kt index e9719c6..fa7caef 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/oauth/OAuthHelper.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/oauth/OAuthHelper.kt @@ -21,7 +21,7 @@ abstract class OAuthHelper { protected get() = WebappManager.getMandatoryProperty("oauth." + name + ".secret") protected val redirectURI: String? protected get() = try { - val uri: String = WebappManager.Companion.getProperty("webapp.url") + "/oauth.html" + val uri: String = WebappManager.getProperty("webapp.external.url") + "/oauth.html" URLEncoder.encode(uri, "UTF-8") } catch (uee: UnsupportedEncodingException) { logger.error("could not encode redirect URI", uee) diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/ApiServlet.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/ApiServlet.kt index 9ebaa0a..42c5664 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/ApiServlet.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/ApiServlet.kt @@ -62,7 +62,7 @@ class ApiServlet : HttpServlet() { // validate request - if ("dev" == WebappManager.getProperty("webapp.env")) { + if ("dev" == WebappManager.getProperty("env")) { response.addHeader("Access-Control-Allow-Origin", "*") } validateAccept(request); diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/WebappManager.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/WebappManager.kt index 3764ffd..153474a 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/WebappManager.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/server/WebappManager.kt @@ -62,7 +62,7 @@ class WebappManager : ServletContextListener, ServletContextAttributeListener, H properties[(key as String).removePrefix(PAIRGOTH_PROPERTIES_PREFIX)] = value } - val env = properties.getProperty("webapp.env") + val env = properties.getProperty("env") logger.info("Using profile $env", ) // set system user agent string to empty string @@ -116,10 +116,10 @@ class WebappManager : ServletContextListener, ServletContextAttributeListener, H return properties.getProperty(prop) } fun getMandatoryProperty(prop: String): String { - return properties.getProperty(prop) ?: throw Error("missing property: ${prop}") + return getProperty(prop) ?: throw Error("missing property: ${prop}") } - val webappURL by lazy { getProperty("webapp.url") } + val webappURL by lazy { getProperty("webapp.external.url") } private val services = mutableMapOf>() diff --git a/api-webapp/src/main/webapp/WEB-INF/pairgoth.default.properties b/api-webapp/src/main/webapp/WEB-INF/pairgoth.default.properties index 621f983..63e2350 100644 --- a/api-webapp/src/main/webapp/WEB-INF/pairgoth.default.properties +++ b/api-webapp/src/main/webapp/WEB-INF/pairgoth.default.properties @@ -1,6 +1,19 @@ -# webapp -webapp.env = dev -webapp.url = https://localhost:8085 +# environment +env = dev + +# webapp connector +webapp.protocol = http +webapp.interface = localhost +webapp.port = 8080 +webapp.context = / +webapp.external.url = http://localhost:8080 + +# api connector +api.protocol = http +api.interface = localhost +api.port = 8085 +api.context = /api +api.external.url = http://localhost:8085/api # store store = file diff --git a/pom.xml b/pom.xml index 7043b8d..d794366 100644 --- a/pom.xml +++ b/pom.xml @@ -8,53 +8,7 @@ 1.0-SNAPSHOT pom - - - default-properties - - true - - - dev - http://localhost:8085 - dev - http://localhost:8080 - - - - custom-properties - - - ../pairgoth.properties - - - - - - org.codehaus.mojo - properties-maven-plugin - 1.2.0 - - - initialize - - read-project-properties - - - - - ../pairgoth.properties - - - - - - - - - - - + central diff --git a/view-webapp/pom.xml b/view-webapp/pom.xml index 9ca66b0..3fd4c90 100644 --- a/view-webapp/pom.xml +++ b/view-webapp/pom.xml @@ -70,50 +70,31 @@ ${project.build.directory}/generated-resources/css - org.eclipse.jetty jetty-maven-plugin ${jetty.version} - 1 + 0 ${pairgoth.webapp.host} ${pairgoth.webapp.port} - + ${pairgoth.webapp.context}/ ${project.basedir}/src/main/webapp,${project.build.directory}/generated-resources/ - org.apache.maven.plugins maven-war-plugin diff --git a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/oauth/OAuthHelper.kt b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/oauth/OAuthHelper.kt index 453a7a4..2c964c2 100644 --- a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/oauth/OAuthHelper.kt +++ b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/oauth/OAuthHelper.kt @@ -21,7 +21,7 @@ abstract class OAuthHelper { protected get() = WebappManager.getMandatoryProperty("oauth." + name + ".secret") protected val redirectURI: String? protected get() = try { - val uri: String = WebappManager.Companion.getProperty("webapp.url") + "/oauth.html" + val uri: String = WebappManager.Companion.getProperty("webapp.external.url") + "/oauth.html" URLEncoder.encode(uri, "UTF-8") } catch (uee: UnsupportedEncodingException) { logger.error("could not encode redirect URI", uee) diff --git a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/view/ApiTool.kt b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/view/ApiTool.kt index 7c498c7..f17b06c 100644 --- a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/view/ApiTool.kt +++ b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/view/ApiTool.kt @@ -11,8 +11,8 @@ class ApiTool { companion object { const val JSON = "application/json" val apiRoot = - System.getProperty("pairgoth.api.url")?.let { "${it.removeSuffix("/")}/" } - ?: System.getProperty("pairgoth.webapp.url")?.let { "${it.removeSuffix("/")}/api/" } + System.getProperty("pairgoth.api.external.url")?.let { "${it.removeSuffix("/")}/" } + ?: System.getProperty("pairgoth.webapp.external.url")?.let { "${it.removeSuffix("/")}/api/" } ?: throw Error("no configured API url") } private val client = OkHttpClient() 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 9f89aa1..f19e75d 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 @@ -63,11 +63,11 @@ class WebappManager : ServletContextListener, ServletContextAttributeListener, H properties[(key as String).removePrefix(PAIRGOTH_PROPERTIES_PREFIX)] = value } - val env = properties.getProperty("webapp.env") + val env = properties.getProperty("env") logger.info("Using profile {}", ) // let the view be aware of the environment - context.setAttribute("webapp.env", env) + context.setAttribute("env", env) // set system user agent string to empty string System.setProperty("http.agent", "") @@ -114,10 +114,10 @@ class WebappManager : ServletContextListener, ServletContextAttributeListener, H return properties.getProperty(prop) } fun getMandatoryProperty(prop: String): String { - return properties.getProperty(prop) ?: throw Error("missing property: ${prop}") + return getProperty(prop) ?: throw Error("missing property: ${prop}") } - val webappURL by lazy { getProperty("webapp.url") } + val webappURL by lazy { getProperty("webapp.external.url") } private val services = mutableMapOf>() 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 83d736e..6a7fb4d 100644 --- a/webserver/src/main/kotlin/org/jeudego/pairgoth/application/Pairgoth.kt +++ b/webserver/src/main/kotlin/org/jeudego/pairgoth/application/Pairgoth.kt @@ -117,7 +117,7 @@ private fun getResourceProperty(key: String) = serverProps.getProperty(key)?.let private fun launchServer() { // create webapps contexts val webAppContexts = mutableListOf() - val mode = serverProps["mode"] ?: throw Error("missing property: mode") + val mode = serverProps["mode"] ?: "standalone" if (mode == "server" || mode == "standalone") webAppContexts.add(createContext("api", "/api")) if (mode == "client" || mode == "standalone") webAppContexts.add(createContext("view", "/"))