diff options
author | Kristóf Marussy <kristof@marussy.com> | 2023-09-04 18:08:59 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2023-09-04 20:33:55 +0200 |
commit | 0e7c55f7ab8d496e81a3dbd53f14e0c46cb27fa6 (patch) | |
tree | 3df27544768e96ec8508452d07b9c20c30f374b0 | |
parent | refactor(frontend): graph visualizer performance (diff) | |
download | refinery-0e7c55f7ab8d496e81a3dbd53f14e0c46cb27fa6.tar.gz refinery-0e7c55f7ab8d496e81a3dbd53f14e0c46cb27fa6.tar.zst refinery-0e7c55f7ab8d496e81a3dbd53f14e0c46cb27fa6.zip |
refactor: server environemntal variables
* Prefix each variable with REFINERY_
* If not public host is specified, allow all origings and compute the WebSocket
address on the client from the origin.
7 files changed, 30 insertions, 22 deletions
diff --git a/docker/Dockerfile b/docker/Dockerfile index ac164cb7..1485e95b 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile | |||
@@ -39,7 +39,7 @@ FROM refinery-$TARGETARCH | |||
39 | # Layer with platform-independent application jars. | 39 | # Layer with platform-independent application jars. |
40 | ADD --link app_lib /app/lib | 40 | ADD --link app_lib /app/lib |
41 | # Common settings added on top. | 41 | # Common settings added on top. |
42 | ENV LISTEN_ADDRESS=0.0.0.0 LISTEN_PORT=8888 PUBLIC_HOST=localhost PUBLIC_PORT=8888 | 42 | ENV REFINERY_LISTEN_HOST=0.0.0.0 REFINERY_LISTEN_PORT=8888 |
43 | EXPOSE 8888 | 43 | EXPOSE 8888 |
44 | USER 1000 | 44 | USER 1000 |
45 | WORKDIR /app | 45 | WORKDIR /app |
diff --git a/subprojects/frontend/config/detectDevModeOptions.ts b/subprojects/frontend/config/detectDevModeOptions.ts index 665204dc..6052e047 100644 --- a/subprojects/frontend/config/detectDevModeOptions.ts +++ b/subprojects/frontend/config/detectDevModeOptions.ts | |||
@@ -30,8 +30,8 @@ function detectListenOptions( | |||
30 | fallbackHost: string, | 30 | fallbackHost: string, |
31 | fallbackPort: number, | 31 | fallbackPort: number, |
32 | ): ListenOptions { | 32 | ): ListenOptions { |
33 | const host = process.env[`${name}_HOST`] ?? fallbackHost; | 33 | const host = process.env[`REFINERY_${name}_HOST`] ?? fallbackHost; |
34 | const rawPort = process.env[`${name}_PORT`]; | 34 | const rawPort = process.env[`REFINERY_${name}_PORT`]; |
35 | const port = rawPort === undefined ? fallbackPort : parseInt(rawPort, 10); | 35 | const port = rawPort === undefined ? fallbackPort : parseInt(rawPort, 10); |
36 | const secure = port === 443; | 36 | const secure = port === 443; |
37 | return { host, port, secure }; | 37 | return { host, port, secure }; |
diff --git a/subprojects/frontend/src/xtext/BackendConfig.ts b/subprojects/frontend/src/xtext/BackendConfig.ts index 4c7eac5f..e7043bd5 100644 --- a/subprojects/frontend/src/xtext/BackendConfig.ts +++ b/subprojects/frontend/src/xtext/BackendConfig.ts | |||
@@ -11,7 +11,7 @@ import { z } from 'zod'; | |||
11 | export const ENDPOINT = 'config.json'; | 11 | export const ENDPOINT = 'config.json'; |
12 | 12 | ||
13 | const BackendConfig = z.object({ | 13 | const BackendConfig = z.object({ |
14 | webSocketURL: z.string().url(), | 14 | webSocketURL: z.string().url().optional(), |
15 | }); | 15 | }); |
16 | 16 | ||
17 | type BackendConfig = z.infer<typeof BackendConfig>; | 17 | type BackendConfig = z.infer<typeof BackendConfig>; |
diff --git a/subprojects/frontend/src/xtext/XtextWebSocketClient.ts b/subprojects/frontend/src/xtext/XtextWebSocketClient.ts index 6bb7eec8..963c1d4c 100644 --- a/subprojects/frontend/src/xtext/XtextWebSocketClient.ts +++ b/subprojects/frontend/src/xtext/XtextWebSocketClient.ts | |||
@@ -282,7 +282,10 @@ export default class XtextWebSocketClient { | |||
282 | log.debug('Creating WebSocket'); | 282 | log.debug('Creating WebSocket'); |
283 | 283 | ||
284 | (async () => { | 284 | (async () => { |
285 | const { webSocketURL } = await fetchBackendConfig(); | 285 | let { webSocketURL } = await fetchBackendConfig(); |
286 | if (webSocketURL === undefined) { | ||
287 | webSocketURL = `${window.origin.replace(/^http/, 'ws')}/xtext-service`; | ||
288 | } | ||
286 | this.openWebSocketWithURL(webSocketURL); | 289 | this.openWebSocketWithURL(webSocketURL); |
287 | })().catch((error) => { | 290 | })().catch((error) => { |
288 | log.error('Error while initializing connection', error); | 291 | log.error('Error while initializing connection', error); |
diff --git a/subprojects/language-web/build.gradle.kts b/subprojects/language-web/build.gradle.kts index 9f772d41..88dccdf3 100644 --- a/subprojects/language-web/build.gradle.kts +++ b/subprojects/language-web/build.gradle.kts | |||
@@ -64,8 +64,18 @@ tasks { | |||
64 | classpath(mainRuntimeClasspath) | 64 | classpath(mainRuntimeClasspath) |
65 | mainClass.set(application.mainClass) | 65 | mainClass.set(application.mainClass) |
66 | standardInput = System.`in` | 66 | standardInput = System.`in` |
67 | environment("BASE_RESOURCE", webapp.singleFile) | 67 | environment("REFINERY_BASE_RESOURCE", webapp.singleFile) |
68 | group = "run" | 68 | group = "run" |
69 | description = "Start a Jetty web server serving the Xtext API and assets." | 69 | description = "Start a Jetty web server serving the Xtext API and assets." |
70 | } | 70 | } |
71 | |||
72 | register<JavaExec>("serveBackendOnly") { | ||
73 | val mainRuntimeClasspath = sourceSets.main.map { it.runtimeClasspath } | ||
74 | dependsOn(mainRuntimeClasspath) | ||
75 | classpath(mainRuntimeClasspath) | ||
76 | mainClass.set(application.mainClass) | ||
77 | standardInput = System.`in` | ||
78 | group = "run" | ||
79 | description = "Start a Jetty web server serving the Xtext API without assets." | ||
80 | } | ||
71 | } | 81 | } |
diff --git a/subprojects/language-web/src/main/java/tools/refinery/language/web/ServerLauncher.java b/subprojects/language-web/src/main/java/tools/refinery/language/web/ServerLauncher.java index 3e1d811b..d633b3fc 100644 --- a/subprojects/language-web/src/main/java/tools/refinery/language/web/ServerLauncher.java +++ b/subprojects/language-web/src/main/java/tools/refinery/language/web/ServerLauncher.java | |||
@@ -33,7 +33,7 @@ import java.util.EnumSet; | |||
33 | import java.util.Set; | 33 | import java.util.Set; |
34 | 34 | ||
35 | public class ServerLauncher { | 35 | public class ServerLauncher { |
36 | public static final String DEFAULT_LISTEN_ADDRESS = "localhost"; | 36 | public static final String DEFAULT_LISTEN_HOST = "localhost"; |
37 | 37 | ||
38 | public static final int DEFAULT_LISTEN_PORT = 1312; | 38 | public static final int DEFAULT_LISTEN_PORT = 1312; |
39 | 39 | ||
@@ -105,7 +105,7 @@ public class ServerLauncher { | |||
105 | 105 | ||
106 | private Resource getBaseResource() { | 106 | private Resource getBaseResource() { |
107 | var factory = ResourceFactory.of(server); | 107 | var factory = ResourceFactory.of(server); |
108 | var baseResourceOverride = System.getenv("BASE_RESOURCE"); | 108 | var baseResourceOverride = System.getenv("REFINERY_BASE_RESOURCE"); |
109 | if (baseResourceOverride != null) { | 109 | if (baseResourceOverride != null) { |
110 | // If a user override is provided, use it. | 110 | // If a user override is provided, use it. |
111 | return factory.newResource(baseResourceOverride); | 111 | return factory.newResource(baseResourceOverride); |
@@ -155,15 +155,15 @@ public class ServerLauncher { | |||
155 | } | 155 | } |
156 | 156 | ||
157 | private static String getListenAddress() { | 157 | private static String getListenAddress() { |
158 | var listenAddress = System.getenv("LISTEN_ADDRESS"); | 158 | var listenAddress = System.getenv("REFINERY_LISTEN_HOST"); |
159 | if (listenAddress == null) { | 159 | if (listenAddress == null) { |
160 | return DEFAULT_LISTEN_ADDRESS; | 160 | return DEFAULT_LISTEN_HOST; |
161 | } | 161 | } |
162 | return listenAddress; | 162 | return listenAddress; |
163 | } | 163 | } |
164 | 164 | ||
165 | private static int getListenPort() { | 165 | private static int getListenPort() { |
166 | var portStr = System.getenv("LISTEN_PORT"); | 166 | var portStr = System.getenv("REFINERY_LISTEN_PORT"); |
167 | if (portStr != null) { | 167 | if (portStr != null) { |
168 | return Integer.parseInt(portStr); | 168 | return Integer.parseInt(portStr); |
169 | } | 169 | } |
@@ -177,7 +177,7 @@ public class ServerLauncher { | |||
177 | } | 177 | } |
178 | 178 | ||
179 | private static String getPublicHost() { | 179 | private static String getPublicHost() { |
180 | var publicHost = System.getenv("PUBLIC_HOST"); | 180 | var publicHost = System.getenv("REFINERY_PUBLIC_HOST"); |
181 | if (publicHost != null) { | 181 | if (publicHost != null) { |
182 | return publicHost.toLowerCase(); | 182 | return publicHost.toLowerCase(); |
183 | } | 183 | } |
@@ -185,7 +185,7 @@ public class ServerLauncher { | |||
185 | } | 185 | } |
186 | 186 | ||
187 | private static int getPublicPort() { | 187 | private static int getPublicPort() { |
188 | var portStr = System.getenv("PUBLIC_PORT"); | 188 | var portStr = System.getenv("REFINERY_PUBLIC_PORT"); |
189 | if (portStr != null) { | 189 | if (portStr != null) { |
190 | return Integer.parseInt(portStr); | 190 | return Integer.parseInt(portStr); |
191 | } | 191 | } |
@@ -193,7 +193,7 @@ public class ServerLauncher { | |||
193 | } | 193 | } |
194 | 194 | ||
195 | private static String[] getAllowedOrigins() { | 195 | private static String[] getAllowedOrigins() { |
196 | var allowedOrigins = System.getenv("ALLOWED_ORIGINS"); | 196 | var allowedOrigins = System.getenv("REFINERY_ALLOWED_ORIGINS"); |
197 | if (allowedOrigins != null) { | 197 | if (allowedOrigins != null) { |
198 | return allowedOrigins.split(ALLOWED_ORIGINS_SEPARATOR); | 198 | return allowedOrigins.split(ALLOWED_ORIGINS_SEPARATOR); |
199 | } | 199 | } |
@@ -222,12 +222,10 @@ public class ServerLauncher { | |||
222 | int port; | 222 | int port; |
223 | var publicHost = getPublicHost(); | 223 | var publicHost = getPublicHost(); |
224 | if (publicHost == null) { | 224 | if (publicHost == null) { |
225 | host = getListenAddress(); | 225 | return null; |
226 | port = getListenPort(); | ||
227 | } else { | ||
228 | host = publicHost; | ||
229 | port = getPublicPort(); | ||
230 | } | 226 | } |
227 | host = publicHost; | ||
228 | port = getPublicPort(); | ||
231 | var scheme = port == HTTPS_DEFAULT_PORT ? "wss" : "ws"; | 229 | var scheme = port == HTTPS_DEFAULT_PORT ? "wss" : "ws"; |
232 | return String.format("%s://%s:%d/xtext-service", scheme, host, port); | 230 | return String.format("%s://%s:%d/xtext-service", scheme, host, port); |
233 | } | 231 | } |
diff --git a/subprojects/language-web/src/main/java/tools/refinery/language/web/config/BackendConfigServlet.java b/subprojects/language-web/src/main/java/tools/refinery/language/web/config/BackendConfigServlet.java index a2f04e34..7d0a5122 100644 --- a/subprojects/language-web/src/main/java/tools/refinery/language/web/config/BackendConfigServlet.java +++ b/subprojects/language-web/src/main/java/tools/refinery/language/web/config/BackendConfigServlet.java | |||
@@ -25,9 +25,6 @@ public class BackendConfigServlet extends HttpServlet { | |||
25 | public void init(ServletConfig config) throws ServletException { | 25 | public void init(ServletConfig config) throws ServletException { |
26 | super.init(config); | 26 | super.init(config); |
27 | var webSocketUrl = config.getInitParameter(WEBSOCKET_URL_INIT_PARAM); | 27 | var webSocketUrl = config.getInitParameter(WEBSOCKET_URL_INIT_PARAM); |
28 | if (webSocketUrl == null) { | ||
29 | throw new IllegalArgumentException("Init parameter " + WEBSOCKET_URL_INIT_PARAM + " is mandatory"); | ||
30 | } | ||
31 | var backendConfig = new BackendConfig(webSocketUrl); | 28 | var backendConfig = new BackendConfig(webSocketUrl); |
32 | var gson = new Gson(); | 29 | var gson = new Gson(); |
33 | serializedConfig = gson.toJson(backendConfig); | 30 | serializedConfig = gson.toJson(backendConfig); |