From 892844644461e2fabc7b13eca56f240a5d239da5 Mon Sep 17 00:00:00 2001 From: jay-tux Date: Mon, 24 Aug 2026 18:17:37 +0200 Subject: [PATCH] Improve WASM client reusability --- .gitignore | 4 +- README.md | 62 +++++++++++++++++++ clients/compose/build.gradle.kts | 12 ---- .../com/jaytux/phoebench/clients/Main.kt | 4 +- .../phoebench/clients/PlatformAPI.wasmJs.kt | 17 ++++- 5 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 README.md diff --git a/.gitignore b/.gitignore index 13840fd..f1810b8 100644 --- a/.gitignore +++ b/.gitignore @@ -47,4 +47,6 @@ bin/ *.db .env .idea/ -artifacts/ \ No newline at end of file +artifacts/ + +clients/compose/src/wasmJsMain/resources/config.json \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..3c9c6a1 --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +# PhoeBench +*Introduction to be done* + +## Setup +### Server +The server ZIP files in the releases contain all dependencies, so just unzip it where you want to run the server. + +First, set up the environment variables (this cannot currently be done using `.env` files, I think): +- `JWT_AUDIENCE`: the audience to use for the JSON Web Tokens authentication, typically set to your URL; +- `JWT_ISSUER`: the issuer for the authentication tokens, typically also set to your URL; +- `JWT_REALM`: the realm for which the authentication tokens are valid, doesn't matter as there is only one "realm" (I use `phoebench`); +- `JWT_SECRET`: a strong secret key to encrypt the authentication tokens (e.g. generated by `gpg --gen-random --armor 1 128`); +- `PORT`: the port to host the server on; +- `PHOEBENCH_SAFE_CLIENT`: (optional) if you plan on hosting a web client, set this to its URL (for CORS requests); +- `PHOEBENCH_DEV_CLIENT_PORT`: (development only) port to enable CORS requests from `localhost` and `127.0.0.1`. + +Next, set up the database variables (recommended through a `.env` file): +- `DATABASE_URL`: JDBC URL to connect to the database; +- `DATABASE_DRIVER`: fully-qualified name of the JDBC driver to connect to the database with; +- `DATABASE_USER`: (if needed) username for the database; +- `DATABASE_PASSWORD`: (if needed) password for the database. + +The server currently supports SQLite and MariaDB as databases, using the following settings: +``` +# SQLite +DATABASE_URL=jdbc:sqlite:/path/to/db/file.db +DATABASE_DRIVER=org.sqlite.JDBC +# SQLite does not need user/password + +# MariaDB +DATABASE_URL=jdbc:mariadb://host:port/database-name +DATABASE_DRIVER=org.mariadb.jdbc.Driver +DATABASE_USER=username +DATABASE_PASSWORD=pass +``` +For SQLite, don't forget to substitute `/path/to/db/file.db` (with the path to where the database should be stored). +For MariaDB, don't forget to substitute `host`, `port`, `database-name`, `username`, and `pass` (`host:port` will typically be `localhost:3306`). +To use MariaDB, you will need to create the database and user through its administration interface. + +### JVM Client +The JVM client does not require any additional setup. +Simply unzip the client ZIP for your platform (currently only built for Linux), and run `bin/com.jaytux.phoebench.clients` from the unzip location. + +However, to avoid having to log in again every time you start the app, it is recommended to ensure you have a working keyring set up. + +### WASM Client +The WASM/JS client ZIP contains files that can be statically served, including all dependencies. +As the WASM files can take some time to load over the network, it is recommended to modify the `index.html` file to contain some sort of loading screen. + +As for setup - you need to create a file called `config.json` with the following content: +```json +{ + "server": "server url" +} +``` +Replace `server url` with the URL of the server to connect to (don't forget to add it as `PHOEBENCH_SAFE_CLIENT` to the server, see above). +For deployment, this file should be in the same directory where the ZIP was extracted. +For development, it should be under `/clients/compose/src/wasmJsMain/resources/` to ensure correct loading. + +### CLI Client +The CLI client does not require any additional setup, simply use Java to run the JAR: `java -jar phoebench-cli.jar`. +To use the CLI client in scripts, it is recommended to suppress certain warnings: `java --enable-native-access=ALL_UNNAMED -jar phoebench-cli.jar`, as it will otherwise output warning messages that might trip up your scripts. \ No newline at end of file diff --git a/clients/compose/build.gradle.kts b/clients/compose/build.gradle.kts index eafda37..d595efd 100644 --- a/clients/compose/build.gradle.kts +++ b/clients/compose/build.gradle.kts @@ -154,16 +154,4 @@ fun envValue(key: String): String? { } .firstOrNull { it.first == key }?.second } -} - -buildConfig { - sourceSets.getByName("wasmJsMain") { - val serverUrl = envValue("WASM_SERVER_URL") ?: - throw GradleException("MISSING CONFIGURATION: WASM_SERVER_URL. Please set it as environment variable or `.env` variable.") - - className("WasmConfig") - packageName("com.jaytux.phoebench.clients") - useKotlinOutput() - buildConfigField("SERVER_URL", serverUrl) - } } \ No newline at end of file diff --git a/clients/compose/src/wasmJsMain/kotlin/com/jaytux/phoebench/clients/Main.kt b/clients/compose/src/wasmJsMain/kotlin/com/jaytux/phoebench/clients/Main.kt index 5b64c94..f1f4928 100644 --- a/clients/compose/src/wasmJsMain/kotlin/com/jaytux/phoebench/clients/Main.kt +++ b/clients/compose/src/wasmJsMain/kotlin/com/jaytux/phoebench/clients/Main.kt @@ -1,11 +1,13 @@ package com.jaytux.phoebench.clients +import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.window.ComposeViewport import kotlinx.browser.document @OptIn(ExperimentalComposeUiApi::class) -fun main() { +suspend fun main() { + config = loadConfig() ComposeViewport(document.body!!) { App() } diff --git a/clients/compose/src/wasmJsMain/kotlin/com/jaytux/phoebench/clients/PlatformAPI.wasmJs.kt b/clients/compose/src/wasmJsMain/kotlin/com/jaytux/phoebench/clients/PlatformAPI.wasmJs.kt index 2400806..035e4fe 100644 --- a/clients/compose/src/wasmJsMain/kotlin/com/jaytux/phoebench/clients/PlatformAPI.wasmJs.kt +++ b/clients/compose/src/wasmJsMain/kotlin/com/jaytux/phoebench/clients/PlatformAPI.wasmJs.kt @@ -6,7 +6,10 @@ import com.jaytux.phoebench.clients.ui.constantServerSelect import io.ktor.client.* import io.ktor.client.engine.js.* import kotlinx.browser.window +import kotlinx.coroutines.await import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json import kotlinx.serialization.serializer import kotlin.uuid.Uuid @@ -56,7 +59,19 @@ private external fun jsFormat(num: Double): String actual fun format(f: Float): String = jsFormat(f.toDouble()) +@Serializable +data class Config(val server: String) + +@OptIn(ExperimentalWasmJsInterop::class) +suspend fun loadConfig(): Config { + val resp = window.fetch("config.json").await() + val text = resp.text().await() + return Json.decodeFromString(text.toString()) +} + +lateinit var config: Config + @Composable actual fun ServerSelect(server: String, onUpdate: (String) -> Unit) { - constantServerSelect(WasmConfig.SERVER_URL)(server, onUpdate) + constantServerSelect(config.server)(server, onUpdate) } \ No newline at end of file