diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2022-05-18 13:36:14 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2022-05-18 14:34:54 +0200 |
| commit | 8ec4bd7584ad67b4aebd2a88a1e33902923a5375 (patch) | |
| tree | ea220eabeb4912fd640242277aa8441a1238d791 /opendc-web/opendc-web-ui-quarkus/src | |
| parent | 689c873ff68b6c8fd66740603dad6efa3bf8ab47 (diff) | |
refactor(web/ui): Remove module nesting in Quarkus extension
This change updates the OpenDC web UI Quarkus extension to live
completely in the `opendc-web` directory, as opposed to adding another
level of nesting. This also allows us to properly name the artifacts of
the Quarkus extension modules.
Diffstat (limited to 'opendc-web/opendc-web-ui-quarkus/src')
3 files changed, 151 insertions, 0 deletions
diff --git a/opendc-web/opendc-web-ui-quarkus/src/main/java/org/opendc/web/ui/runtime/OpenDCUiRecorder.java b/opendc-web/opendc-web-ui-quarkus/src/main/java/org/opendc/web/ui/runtime/OpenDCUiRecorder.java new file mode 100644 index 00000000..026a9039 --- /dev/null +++ b/opendc-web/opendc-web-ui-quarkus/src/main/java/org/opendc/web/ui/runtime/OpenDCUiRecorder.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.ui.runtime; + +import io.quarkus.runtime.ShutdownContext; +import io.quarkus.runtime.annotations.Recorder; +import io.quarkus.vertx.http.runtime.devmode.FileSystemStaticHandler; +import io.quarkus.vertx.http.runtime.webjar.WebJarNotFoundHandler; +import io.quarkus.vertx.http.runtime.webjar.WebJarStaticHandler; +import io.vertx.core.Handler; +import io.vertx.ext.web.RoutingContext; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * Helper class for serving the OpenDC web interface. + */ +@Recorder +public class OpenDCUiRecorder { + /** + * Construct a {@link Handler} for serving a page of the OpenDC web interface. + */ + public Handler<RoutingContext> pageHandler( + String finalDestination, + String page, + OpenDCUiRuntimeConfig runtimeConfig + ) { + if (runtimeConfig.enable) { + String pageDirectory = finalDestination + "/pages"; + return (event) -> { + event.response() + .setStatusCode(200) + .sendFile(pageDirectory + page + ".html"); + }; + } + + return new WebJarNotFoundHandler(); + } + + /** + * Construct a {@link Handler} for handling redirects in the OpenDC web interface. + */ + public Handler<RoutingContext> redirectHandler( + String destination, + int statusCode, + OpenDCUiRuntimeConfig runtimeConfig + ) { + if (runtimeConfig.enable) { + return (event) -> { + String query = event.request().query(); + String fullDestination = query != null ? destination + "?" + query : destination; + + event.response() + .setStatusCode(statusCode) + .putHeader("Location", fullDestination) + .end(); + }; + } + + return new WebJarNotFoundHandler(); + } + + /** + * Construct a {@link Handler} for serving the static files of the OpenDC web interface. + */ + public Handler<RoutingContext> staticHandler( + String finalDestination, + String path, + List<FileSystemStaticHandler.StaticWebRootConfiguration> webRootConfigurations, + OpenDCUiRuntimeConfig runtimeConfig, + ShutdownContext shutdownContext + ) { + if (runtimeConfig.enable) { + var augmentedWebRootConfigurations = webRootConfigurations + .stream() + .map(c -> new FileSystemStaticHandler.StaticWebRootConfiguration(c.getFileSystem(), c.getWebRoot().isEmpty() ? "static" : c.getWebRoot() + "/static")) + .collect(Collectors.toList()); + + WebJarStaticHandler handler = new WebJarStaticHandler(finalDestination + "/static", path, augmentedWebRootConfigurations); + shutdownContext.addShutdownTask(new ShutdownContext.CloseRunnable(handler)); + return handler; + } + + return new WebJarNotFoundHandler(); + } +} diff --git a/opendc-web/opendc-web-ui-quarkus/src/main/java/org/opendc/web/ui/runtime/OpenDCUiRuntimeConfig.java b/opendc-web/opendc-web-ui-quarkus/src/main/java/org/opendc/web/ui/runtime/OpenDCUiRuntimeConfig.java new file mode 100644 index 00000000..8ae3b6a2 --- /dev/null +++ b/opendc-web/opendc-web-ui-quarkus/src/main/java/org/opendc/web/ui/runtime/OpenDCUiRuntimeConfig.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.ui.runtime; + +import io.quarkus.runtime.annotations.ConfigItem; +import io.quarkus.runtime.annotations.ConfigPhase; +import io.quarkus.runtime.annotations.ConfigRoot; + +/** + * Configuration for the OpenDC web UI. + */ +@ConfigRoot(phase = ConfigPhase.RUN_TIME, name = "opendc-ui") +public class OpenDCUiRuntimeConfig { + /** + * Flag to indicate whether the web interface should be served by the OpenDC API server. + */ + @ConfigItem(defaultValue = "true") + public boolean enable; +} diff --git a/opendc-web/opendc-web-ui-quarkus/src/main/resources/META-INF/quarkus-extension.yaml b/opendc-web/opendc-web-ui-quarkus/src/main/resources/META-INF/quarkus-extension.yaml new file mode 100644 index 00000000..581a1779 --- /dev/null +++ b/opendc-web/opendc-web-ui-quarkus/src/main/resources/META-INF/quarkus-extension.yaml @@ -0,0 +1,5 @@ +--- +name: "OpenDC Web UI" +metadata: + status: "preview" + unlisted: true |
