From b3f0e768cd9fb51b76b9dc021b0534e0e2354d5b Mon Sep 17 00:00:00 2001 From: David Durieux Date: Sun, 10 May 2026 08:15:03 +0200 Subject: [PATCH 1/3] Change JWT token to 24h and refresh token to 30 days --- keycloak/realm/dyingstar-realm.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/keycloak/realm/dyingstar-realm.json b/keycloak/realm/dyingstar-realm.json index 8463026..f792ee6 100644 --- a/keycloak/realm/dyingstar-realm.json +++ b/keycloak/realm/dyingstar-realm.json @@ -21,10 +21,10 @@ "defaultSignatureAlgorithm": "RS256", "revokeRefreshToken": false, "refreshTokenMaxReuse": 0, - "ssoSessionIdleTimeout": 1800, - "ssoSessionMaxLifespan": 36000, - "accessTokenLifespan": 300, - "accessTokenLifespanForImplicitFlow": 900, + "ssoSessionIdleTimeout": 2592000, + "ssoSessionMaxLifespan": 2592000, + "accessTokenLifespan": 86400, + "accessTokenLifespanForImplicitFlow": 86400, "passwordPolicy": "length(10) and notUsername and notEmail and passwordHistory(3) and forceExpiredPasswordChange(365)", "internationalizationEnabled": true, "supportedLocales": ["en", "fr"], From e7d75f93e8014431415994a0222bde219517e3c2 Mon Sep 17 00:00:00 2001 From: David Durieux Date: Thu, 14 May 2026 12:08:38 +0200 Subject: [PATCH 2/3] Add gitignore --- .gitignore | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..45ff0de --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/.vs +/.vscode + +# will have compiled files and executables +persistence/debug/ +persistence/target/ + +persistence/.cargo +persistence/Cargo.lock From 020923caff800d8c2a2255b8a4e31739c4a73e7a Mon Sep 17 00:00:00 2001 From: David Durieux Date: Thu, 14 May 2026 12:11:01 +0200 Subject: [PATCH 3/3] Add persistence service --- .github/workflows/build-pr-persistence.yaml | 18 + .../workflows/build-preprod-persistence.yaml | 25 + .github/workflows/build-prod-persistence.yaml | 37 + persistence/.env.example | 17 + persistence/Cargo.lock | 1836 +++++++++++++++++ persistence/Cargo.toml | 46 + persistence/Makefile | 55 + persistence/docker/.dockerignore | 2 + persistence/docker/Dockerfile | 41 + persistence/docker/docker-compose.yml | 39 + persistence/openapi.yaml | 322 +++ persistence/src/cache/mod.rs | 89 + persistence/src/config.rs | 62 + persistence/src/db/connection.rs | 54 + persistence/src/db/mod.rs | 2 + persistence/src/db/queries.rs | 211 ++ persistence/src/error.rs | 46 + persistence/src/main.rs | 127 ++ persistence/src/rest/handlers.rs | 246 +++ persistence/src/rest/mod.rs | 21 + persistence/src/websocket/handlers.rs | 245 +++ persistence/src/websocket/messages.rs | 60 + persistence/src/websocket/mod.rs | 3 + persistence/src/websocket/server.rs | 55 + 24 files changed, 3659 insertions(+) create mode 100644 .github/workflows/build-pr-persistence.yaml create mode 100644 .github/workflows/build-preprod-persistence.yaml create mode 100644 .github/workflows/build-prod-persistence.yaml create mode 100644 persistence/.env.example create mode 100644 persistence/Cargo.lock create mode 100644 persistence/Cargo.toml create mode 100644 persistence/Makefile create mode 100644 persistence/docker/.dockerignore create mode 100644 persistence/docker/Dockerfile create mode 100644 persistence/docker/docker-compose.yml create mode 100644 persistence/openapi.yaml create mode 100644 persistence/src/cache/mod.rs create mode 100644 persistence/src/config.rs create mode 100644 persistence/src/db/connection.rs create mode 100644 persistence/src/db/mod.rs create mode 100644 persistence/src/db/queries.rs create mode 100644 persistence/src/error.rs create mode 100644 persistence/src/main.rs create mode 100644 persistence/src/rest/handlers.rs create mode 100644 persistence/src/rest/mod.rs create mode 100644 persistence/src/websocket/handlers.rs create mode 100644 persistence/src/websocket/messages.rs create mode 100644 persistence/src/websocket/mod.rs create mode 100644 persistence/src/websocket/server.rs diff --git a/.github/workflows/build-pr-persistence.yaml b/.github/workflows/build-pr-persistence.yaml new file mode 100644 index 0000000..0f0147d --- /dev/null +++ b/.github/workflows/build-pr-persistence.yaml @@ -0,0 +1,18 @@ +name: "PR Build Check: persistence" + +on: + pull_request: + paths: + - "persistence/**" + +jobs: + build: + name: Build persistence (PR check) + uses: ./.github/workflows/_build-push.yaml + with: + service_name: persistence + context_path: persistence + dockerfile_path: persistence/docker/Dockerfile + image_name: local/persistence + image_tag: pr-check + push: false diff --git a/.github/workflows/build-preprod-persistence.yaml b/.github/workflows/build-preprod-persistence.yaml new file mode 100644 index 0000000..20bac6f --- /dev/null +++ b/.github/workflows/build-preprod-persistence.yaml @@ -0,0 +1,25 @@ +name: "Build & Deploy to Preprod: persistence" + +on: + push: + branches: + - develop + paths: + - "persistence/**" + +jobs: + build: + name: Build persistence (preprod) + uses: ./.github/workflows/_build-push.yaml + with: + service_name: persistence + context_path: persistence + dockerfile_path: persistence/docker/Dockerfile + image_name: harbor.dyingstar-game.space/dyingstar/persistence + image_tag: develop + chart_name: service-persistence + trigger_preprod_deploy: true + secrets: + HARBOR_USERNAME: ${{ secrets.HARBOR_USERNAME }} + HARBOR_PASSWORD: ${{ secrets.HARBOR_PASSWORD }} + KUBERNETES_REPO_TOKEN: ${{ secrets.KUBERNETES_REPO_TOKEN }} diff --git a/.github/workflows/build-prod-persistence.yaml b/.github/workflows/build-prod-persistence.yaml new file mode 100644 index 0000000..53f82d2 --- /dev/null +++ b/.github/workflows/build-prod-persistence.yaml @@ -0,0 +1,37 @@ +name: "Build Production Image: persistence" + +on: + push: + tags: + - "persistence-v*" + +jobs: + extract-version: + name: Extract version from tag + runs-on: ubuntu-latest + outputs: + version: ${{ steps.tag.outputs.version }} + steps: + - name: Extract version + id: tag + run: | + TAG="${GITHUB_REF_NAME}" + VERSION="${TAG#persistence-}" + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" + + build: + name: Build persistence (prod) + needs: extract-version + uses: ./.github/workflows/_build-push.yaml + with: + service_name: persistence + context_path: persistence + dockerfile_path: persistence/docker/Dockerfile + image_name: harbor.dyingstar-game.space/dyingstar/persistence + image_tag: ${{ needs.extract-version.outputs.version }} + additional_tags: latest + chart_name: service-persistence + trigger_preprod_deploy: false + secrets: + HARBOR_USERNAME: ${{ secrets.HARBOR_USERNAME }} + HARBOR_PASSWORD: ${{ secrets.HARBOR_PASSWORD }} diff --git a/persistence/.env.example b/persistence/.env.example new file mode 100644 index 0000000..3f78224 --- /dev/null +++ b/persistence/.env.example @@ -0,0 +1,17 @@ +# WebSocket listen port +WS_PORT=9100 + +# ScyllaDB nodes (comma-separated host:port) +SCYLLA_NODES=127.0.0.1:9042 + +# ScyllaDB keyspace +SCYLLA_KEYSPACE=dyingstar + +# How often to flush the in-memory cache to ScyllaDB (seconds) +CACHE_FLUSH_INTERVAL_SECS=60 + +# Number of items sent per WebSocket chunk message +CHUNK_SIZE=1000 + +# Tracing log level (trace|debug|info|warn|error) +RUST_LOG=persistence=info,tower_http=debug diff --git a/persistence/Cargo.lock b/persistence/Cargo.lock new file mode 100644 index 0000000..494048d --- /dev/null +++ b/persistence/Cargo.lock @@ -0,0 +1,1836 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anyhow" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + +[[package]] +name = "arc-swap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a3a1fd6f75306b68087b831f025c712524bcb19aad54e557b1129cfa0a2b207" +dependencies = [ + "rustversion", +] + +[[package]] +name = "async-trait" +version = "0.1.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "axum" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90" +dependencies = [ + "axum-core", + "axum-macros", + "base64", + "bytes", + "form_urlencoded", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde_core", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sha1", + "sync_wrapper", + "tokio", + "tokio-tungstenite", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-macros" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aa268c23bfbbd2c4363b9cd302a4f504fb2a9dfe7e3451d66f35dd392e20aca" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bitflags" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" + +[[package]] +name = "cc" +version = "1.2.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1dce859f0832a7d088c4f1119888ab94ef4b5d6795d1ce05afb7fe159d79f98" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "chrono" +version = "0.4.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0" +dependencies = [ + "iana-time-zone", + "num-traits", + "windows-link", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "darling" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d00b9596d185e565c2207a0b01f8bd1a135483d02d9b7b0a54b11da8d53412e" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "dashmap" +version = "6.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core", +] + +[[package]] +name = "data-encoding" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4ae5f15dda3c708c0ade84bfee31ccab44a3da4f88015ed22f63732abe300c8" + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-executor" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf29c38818342a3b26b5b923639e7b1f4a61fc5e76102d4b1981c6dc7a7579d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718" + +[[package]] +name = "futures-macro" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c39754e157331b013978ec91992bde1ac089843443c49cbc7f46150b0fad0893" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", + "wasip2", + "wasip3", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "http" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6299f016b246a94207e63da54dbe807655bf9e00044f73ded42c3ac5305fbcca" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "bytes", + "http", + "http-body", + "hyper", + "pin-project-lite", + "tokio", + "tower-service", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", + "serde", + "serde_core", +] + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "js-sys" +version = "0.3.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67df7112613f8bfd9150013a0314e196f4800d3201ae742489d999db2f979f08" +dependencies = [ + "cfg-if", + "futures-util", + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "lz4_flex" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "373f5eceeeab7925e0c1098212f2fbc4d416adec9d35051a6ab251e824c1854a" +dependencies = [ + "twox-hash", +] + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mio" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50b7e5b27aa02a74bac8c3f23f448f8d87ff11f92d3aac1a6ed369ee08cc56c1" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "persistence" +version = "0.1.0" +dependencies = [ + "anyhow", + "axum", + "dashmap", + "dotenvy", + "futures", + "scylla", + "serde", + "serde_json", + "thiserror", + "tokio", + "tower", + "tower-http", + "tracing", + "tracing-subscriber", + "uuid", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_pcg" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b48ac3f7ffaab7fac4d2376632268aa5f89abdb55f7ebf8f4d11fffccb2320f7" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "scylla" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0f9ff6ccde6555beec62c794bbc8dcae08a15f2397e057a5978c199174be920" +dependencies = [ + "arc-swap", + "async-trait", + "bytes", + "chrono", + "dashmap", + "futures", + "hashbrown 0.15.5", + "itertools", + "rand", + "rand_pcg", + "scylla-cql", + "smallvec", + "socket2 0.5.10", + "thiserror", + "tokio", + "tracing", + "uuid", +] + +[[package]] +name = "scylla-cql" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1687853d084bd9debb38f326298aeb7ae9c9a336d6f22684f3579db785051efd" +dependencies = [ + "byteorder", + "bytes", + "chrono", + "itertools", + "lz4_flex", + "scylla-macros", + "snap", + "stable_deref_trait", + "thiserror", + "tokio", + "uuid", + "yoke", +] + +[[package]] +name = "scylla-macros" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "619e45d49f95b355afa56840d9021c789eddf8c3a6bef8d4b82de0912097d578" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "snap" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b6b67fb9a61334225b5b790716f609cd58395f895b3fe8b328786812a40bc3b" + +[[package]] +name = "socket2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "socket2" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "tokio" +version = "1.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe" +dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2 0.6.3", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "385a6cb71ab9ab790c5fe8d67f1645e6c450a7ce006a33de03daa956cf70a496" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f72a05e828585856dacd553fba484c242c46e391fb0e58917c942ee9202915c" +dependencies = [ + "futures-util", + "log", + "tokio", + "tungstenite", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-http" +version = "0.6.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68d6fdd9f81c2819c9a8b0e0cd91660e7746a8e6ea2ba7c6b2b057985f6bcb51" +dependencies = [ + "bitflags", + "bytes", + "http", + "http-body", + "pin-project-lite", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "tungstenite" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c01152af293afb9c7c2a57e4b559c5620b421f6d133261c60dd2d0cdb38e6b8" +dependencies = [ + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "rand", + "sha1", + "thiserror", +] + +[[package]] +name = "twox-hash" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ea3136b675547379c4bd395ca6b938e5ad3c3d20fad76e7fe85f9e0d011419c" + +[[package]] +name = "typenum" +version = "1.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40ce102ab67701b8526c123c1bab5cbe42d7040ccfd0f64af1a385808d2f43de" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "uuid" +version = "1.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd74a9687298c6858e9b88ec8935ec45d22e8fd5e6394fa1bd4e99a87789c76" +dependencies = [ + "getrandom 0.4.2", + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.3+wasi-0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20064672db26d7cdc89c7798c48a0fdfac8213434a1186e5ef29fd560ae223d6" +dependencies = [ + "wit-bindgen 0.57.1", +] + +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen 0.51.0", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49ace1d07c165b0864824eee619580c4689389afa9dc9ed3a4c75040d82e6790" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e68e6f4afd367a562002c05637acb8578ff2dea1943df76afb9e83d177c8578" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d95a9ec35c64b2a7cb35d3fead40c4238d0940c86d107136999567a4703259f2" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4e0100b01e9f0d03189a92b96772a1fb998639d981193d7dbab487302513441" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "wit-bindgen-core" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", +] + +[[package]] +name = "yoke" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abe8c5fda708d9ca3df187cae8bfb9ceda00dd96231bed36e445a1a48e66f9ca" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.8.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eed437bf9d6692032087e337407a86f04cd8d6a16a37199ed57949d415bd68e9" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e3cd084b1788766f53af483dd21f93881ff30d7320490ec3ef7526d203bad4" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerofrom" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69faa1f2a1ea75661980b013019ed6687ed0e83d069bc1114e2cc74c6c04c4df" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/persistence/Cargo.toml b/persistence/Cargo.toml new file mode 100644 index 0000000..5247cb8 --- /dev/null +++ b/persistence/Cargo.toml @@ -0,0 +1,46 @@ +[package] +name = "persistence" +version = "0.1.0" +edition = "2021" + +[[bin]] +name = "persistence" +path = "src/main.rs" + +[dependencies] +# Async runtime +tokio = { version = "1", features = ["full"] } + +# Web framework + WebSocket +axum = { version = "0.8", features = ["ws", "macros"] } +tower = "0.5" +tower-http = { version = "0.6", features = ["trace"] } + +# ScyllaDB driver +scylla = "1" + +# Serialization +serde = { version = "1", features = ["derive"] } +serde_json = "1" + +# Concurrent cache +dashmap = "6" + +# Logging / tracing +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } + +# Error handling +thiserror = "2" + +# Environment / config +dotenvy = "0.15" + +# UUID +uuid = { version = "1", features = ["v4"] } + +# Futures utilities +futures = "0.3" + +# Convenient error boxing for main() +anyhow = "1" diff --git a/persistence/Makefile b/persistence/Makefile new file mode 100644 index 0000000..4139457 --- /dev/null +++ b/persistence/Makefile @@ -0,0 +1,55 @@ +# ─── Detect container runtime ───────────────────────────────────────────── +DOCKER_COMPOSE := $(shell command -v podman-compose 2>/dev/null || echo docker compose) +COMPOSE_FILE := docker/docker-compose.yml +ENV_FILE := .env + +# ANSI colours +GREEN := \033[0;32m +YELLOW := \033[0;33m +RESET := \033[0m + +.PHONY: help up down build logs logs-app shell clean + +help: ## Show this help message + @echo "Usage: make " + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ + awk 'BEGIN {FS = ":.*?## "}; {printf " $(GREEN)%-18s$(RESET) %s\n", $$1, $$2}' + +# ─── Environment check ──────────────────────────────────────────────────── +check-env: + @if [ ! -f $(ENV_FILE) ]; then \ + echo "$(YELLOW)Warning: $(ENV_FILE) not found. Copy .env.example to .env and edit it.$(RESET)"; \ + exit 1; \ + fi + +# ─── Container targets ──────────────────────────────────────────────────── +up: check-env ## Start all services (ScyllaDB + app) + $(DOCKER_COMPOSE) -f $(COMPOSE_FILE) up -d + +down: ## Stop and remove containers + $(DOCKER_COMPOSE) -f $(COMPOSE_FILE) down + +build: check-env ## (Re)build the app container image + $(DOCKER_COMPOSE) -f $(COMPOSE_FILE) build app + +logs: ## Tail logs for all services + $(DOCKER_COMPOSE) -f $(COMPOSE_FILE) logs -f + +logs-app: ## Tail logs for the persistence service only + $(DOCKER_COMPOSE) -f $(COMPOSE_FILE) logs -f app + +shell: ## Open a shell inside the running app container + $(DOCKER_COMPOSE) -f $(COMPOSE_FILE) exec app /bin/sh + +# ─── Local dev targets ──────────────────────────────────────────────────── +dev: ## Run locally with cargo (requires ScyllaDB running separately) + cargo run + +release: ## Build a release binary locally + cargo build --release + +test: ## Run tests + cargo test + +clean: ## Remove compiled artifacts + cargo clean diff --git a/persistence/docker/.dockerignore b/persistence/docker/.dockerignore new file mode 100644 index 0000000..14ee500 --- /dev/null +++ b/persistence/docker/.dockerignore @@ -0,0 +1,2 @@ +target/ +.env diff --git a/persistence/docker/Dockerfile b/persistence/docker/Dockerfile new file mode 100644 index 0000000..46a14bb --- /dev/null +++ b/persistence/docker/Dockerfile @@ -0,0 +1,41 @@ +# ─── Stage 1: build ──────────────────────────────────────────────────────── +FROM rust:1.95-slim AS builder + +# System dependencies needed by the scylla driver (openssl, pkg-config) +RUN apt-get update && apt-get install -y \ + pkg-config \ + libssl-dev \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app + +# Cache dependencies separately from source code. +COPY Cargo.toml Cargo.lock* ./ +# Create a stub main so `cargo build --release` caches deps without source. +RUN mkdir -p src && echo 'fn main() {}' > src/main.rs +RUN cargo build --release && rm -rf src + +# Copy real source and rebuild (only changed crates compile again). +COPY src ./src +RUN touch src/main.rs && cargo build --release + +# ─── Stage 2: minimal runtime ────────────────────────────────────────────── +FROM debian:bookworm-slim AS runtime + +RUN apt-get update && apt-get install -y \ + libssl3 \ + ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +# Non-root user for security +RUN groupadd -g 1001 appgroup && useradd -u 1001 -g appgroup -s /bin/sh -m appuser + +WORKDIR /app +COPY --from=builder /app/target/release/persistence ./persistence + +RUN chown -R appuser:appgroup /app +USER appuser + +EXPOSE 9100 + +ENTRYPOINT ["./persistence"] diff --git a/persistence/docker/docker-compose.yml b/persistence/docker/docker-compose.yml new file mode 100644 index 0000000..4a1f5eb --- /dev/null +++ b/persistence/docker/docker-compose.yml @@ -0,0 +1,39 @@ +services: + # ─── Persistence service ─────────────────────────────────────────────────── + app: + build: + context: .. + dockerfile: docker/Dockerfile + env_file: ../.env + ports: + - "${WS_PORT:-9100}:9100" + depends_on: + scylladb: + condition: service_healthy + restart: unless-stopped + networks: + - backend + + # ─── ScyllaDB ───────────────────────────────────────────────────────────── + scylladb: + image: scylladb/scylla:6.2 + command: --smp 1 --memory 512M --developer-mode 1 + ports: + - "9042:9042" + volumes: + - scylla_data:/var/lib/scylla + healthcheck: + test: ["CMD-SHELL", "cqlsh -e 'describe keyspaces' > /dev/null 2>&1"] + interval: 10s + timeout: 10s + retries: 20 + start_period: 60s + networks: + - backend + +volumes: + scylla_data: + +networks: + backend: + driver: bridge diff --git a/persistence/openapi.yaml b/persistence/openapi.yaml new file mode 100644 index 0000000..fca462d --- /dev/null +++ b/persistence/openapi.yaml @@ -0,0 +1,322 @@ +openapi: 3.1.0 +info: + title: DyingStar Persistence REST API + version: 1.0.0 + description: > + REST API for managing generic-props items stored in the DyingStar + persistence service. A second server runs on port 3001 alongside the + WebSocket server (port 9100). + +servers: + - url: http://localhost:3001 + description: Local development + +tags: + - name: items + description: Generic-props item management + +paths: + /items: + get: + tags: [items] + operationId: getItems + summary: Search items + description: > + Return a paginated, optionally filtered list of all items. + Filtering is performed in-process after a full table scan. + parameters: + - $ref: '#/components/parameters/objectTypeFilter' + - $ref: '#/components/parameters/parentIdFilter' + - $ref: '#/components/parameters/scenenameFilter' + - $ref: '#/components/parameters/page' + - $ref: '#/components/parameters/pageSize' + responses: + '200': + description: Paginated list of items + content: + application/json: + schema: + $ref: '#/components/schemas/PaginatedItems' + '500': + $ref: '#/components/responses/InternalError' + + post: + tags: [items] + operationId: createItem + summary: Create an item + description: > + Create a new item. The `object_uuid` in the request body is used as the + item's primary key. Writing through to ScyllaDB and the in-memory cache. + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/CreateItemRequest' + responses: + '201': + description: Item created + content: + application/json: + schema: + $ref: '#/components/schemas/ItemResponse' + '400': + $ref: '#/components/responses/BadRequest' + '500': + $ref: '#/components/responses/InternalError' + + /items/{uuid}: + parameters: + - $ref: '#/components/parameters/uuid' + + get: + tags: [items] + operationId: getItem + summary: Get an item + description: Return a single item by its UUID. + responses: + '200': + description: The requested item + content: + application/json: + schema: + $ref: '#/components/schemas/ItemResponse' + '404': + $ref: '#/components/responses/NotFound' + '500': + $ref: '#/components/responses/InternalError' + + put: + tags: [items] + operationId: putItem + summary: Replace item data + description: > + Fully replace an item's data. The uuid is taken from the URL path. + The response returns the stored item in canonical form. + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/PutItemRequest' + responses: + '200': + description: The updated item + content: + application/json: + schema: + $ref: '#/components/schemas/ItemResponse' + '400': + $ref: '#/components/responses/BadRequest' + '500': + $ref: '#/components/responses/InternalError' + + delete: + tags: [items] + operationId: deleteItem + summary: Delete an item + description: > + Remove an item from the database and the in-memory cache. + Always returns 204 — the operation is idempotent. + responses: + '204': + description: Item deleted (or did not exist) + '500': + $ref: '#/components/responses/InternalError' + +components: + parameters: + uuid: + name: uuid + in: path + required: true + description: Item UUID + schema: + type: string + format: uuid + example: 550e8400-e29b-41d4-a716-446655440000 + + objectTypeFilter: + name: object_type + in: query + required: false + description: Filter by object type (exact match) + schema: + type: string + example: box + + parentIdFilter: + name: parent_id + in: query + required: false + description: Filter by parent UUID (exact match) + schema: + type: string + format: uuid + + scenenameFilter: + name: scenename + in: query + required: false + description: Filter by scene name (exact match) + schema: + type: string + example: scenes/props/testbox/box_50cm.tscn + + page: + name: page + in: query + required: false + description: 1-based page number + schema: + type: integer + minimum: 1 + default: 1 + + pageSize: + name: page_size + in: query + required: false + description: Number of items per page + schema: + type: integer + minimum: 1 + maximum: 10000 + default: 100 + + schemas: + Vec3: + type: object + required: [x, y, z] + properties: + x: + type: number + format: double + example: 10500.0 + y: + type: number + format: double + example: 0.0 + z: + type: number + format: double + example: 10510.0 + + ObjectData: + type: object + description: > + Arbitrary extra data for the object. The well-known keys + (parent_id, scenename, position, rotation) are merged in at + the top level of this object when present. + properties: + parent_id: + type: string + format: uuid + nullable: true + description: UUID of the parent object + example: 3fa85f64-5717-4562-b3fc-2c963f66afa6 + scenename: + type: string + nullable: true + description: Godot scene path for the object + example: scenes/props/testbox/box_50cm.tscn + position: + $ref: '#/components/schemas/Vec3' + rotation: + $ref: '#/components/schemas/Vec3' + additionalProperties: true + + ItemResponse: + type: object + required: [object_type, object_uuid, object_data] + properties: + object_type: + type: string + description: Object type identifier + example: box + object_uuid: + type: string + format: uuid + description: Unique identifier for this item + example: 550e8400-e29b-41d4-a716-446655440000 + object_data: + $ref: '#/components/schemas/ObjectData' + + PutItemRequest: + type: object + required: [object_type, object_data] + properties: + object_type: + type: string + description: Object type identifier + example: box + object_data: + $ref: '#/components/schemas/ObjectData' + + CreateItemRequest: + type: object + required: [object_type, object_uuid, object_data] + properties: + object_type: + type: string + description: Object type identifier + example: box + object_uuid: + type: string + format: uuid + description: UUID for the new item (client-assigned) + example: 550e8400-e29b-41d4-a716-446655440000 + object_data: + $ref: '#/components/schemas/ObjectData' + + PaginatedItems: + type: object + required: [items, total, page, page_size] + properties: + items: + type: array + items: + $ref: '#/components/schemas/ItemResponse' + total: + type: integer + description: Total number of items matching the filters (before pagination) + example: 42 + page: + type: integer + description: Current page number (1-based) + example: 1 + page_size: + type: integer + description: Number of items per page + example: 100 + + ErrorResponse: + type: object + required: [error] + properties: + error: + type: string + description: Human-readable error message + example: "item '550e8400-e29b-41d4-a716-446655440000' not found" + + responses: + NotFound: + description: Resource not found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + BadRequest: + description: Invalid request body + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + + InternalError: + description: Internal server error + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' diff --git a/persistence/src/cache/mod.rs b/persistence/src/cache/mod.rs new file mode 100644 index 0000000..d2799d9 --- /dev/null +++ b/persistence/src/cache/mod.rs @@ -0,0 +1,89 @@ +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, +}; + +use dashmap::DashMap; + +use crate::websocket::messages::Item; + +/// Dual-buffer cache for high-throughput writes. +/// +/// Two `DashMap` instances act as ping-pong buffers: +/// - One is the **active** map: all incoming `update_item` messages land here. +/// - The other is the **idle** map: being flushed to ScyllaDB in the background. +/// +/// The `active` flag indicates which map is currently active: +/// - `false` → `map_a` is active, `map_b` is flushing. +/// - `true` → `map_b` is active, `map_a` is flushing. +/// +/// The swap is atomic and lock-free from the writer perspective: +/// 1. Atomically flip `active`. +/// 2. Drain the now-idle map → return its contents for flushing. +/// 3. Callers continue inserting into the other map immediately. +#[derive(Clone)] +pub struct DualCache { + map_a: Arc>, + map_b: Arc>, + /// `false` → map_a is active; `true` → map_b is active. + active: Arc, +} + +impl DualCache { + pub fn new() -> Self { + Self { + map_a: Arc::new(DashMap::new()), + map_b: Arc::new(DashMap::new()), + active: Arc::new(AtomicBool::new(false)), + } + } + + /// Insert or overwrite an item in the active map. + pub fn insert(&self, item: Item) { + self.active_map().insert(item.uuid.clone(), item); + } + + /// Remove an item by uuid from both buffers. + /// Needed when a DELETE arrives so the item is not re-written on the next flush. + pub fn remove(&self, uuid: &str) { + self.map_a.remove(uuid); + self.map_b.remove(uuid); + } + + /// Atomically swap buffers and return all items that were in the now-idle + /// (previously active) map so they can be flushed to the database. + /// + /// After this call: + /// - The new active map is empty (ready for fresh writes). + /// - The returned `Vec` should be persisted to ScyllaDB. + pub fn swap_and_drain(&self) -> Vec { + // Flip the active flag. Use AcqRel so that all prior writes to the + // active map are visible after the swap. + let was_b = self.active.fetch_xor(true, Ordering::AcqRel); + + // The map that was active is now idle — drain it. + let idle = if was_b { &self.map_b } else { &self.map_a }; + let items: Vec = idle.iter().map(|r| r.value().clone()).collect(); + idle.clear(); + items + } + + fn active_map(&self) -> &DashMap { + if self.active.load(Ordering::Acquire) { + &self.map_b + } else { + &self.map_a + } + } + + /// Number of items currently waiting in the active map. + pub fn pending_count(&self) -> usize { + self.active_map().len() + } +} + +impl Default for DualCache { + fn default() -> Self { + Self::new() + } +} diff --git a/persistence/src/config.rs b/persistence/src/config.rs new file mode 100644 index 0000000..df44728 --- /dev/null +++ b/persistence/src/config.rs @@ -0,0 +1,62 @@ +use std::env; + +/// Application configuration loaded from environment variables. +#[derive(Debug, Clone)] +pub struct Config { + /// Port the WebSocket server listens on. + pub ws_port: u16, + /// Port the REST API server listens on. + pub rest_port: u16, + /// Comma-separated list of ScyllaDB node addresses. + pub scylla_nodes: Vec, + /// ScyllaDB keyspace name. + pub scylla_keyspace: String, + /// How often (in seconds) to flush the cache to ScyllaDB. + pub cache_flush_interval_secs: u64, + /// Number of items per WebSocket chunk message. + pub chunk_size: usize, +} + +impl Config { + /// Load configuration from environment variables. + /// Missing variables fall back to defaults. + pub fn from_env() -> Self { + let ws_port = env::var("WS_PORT") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(9100); + + let rest_port = env::var("REST_PORT") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(3001); + + let scylla_nodes = env::var("SCYLLA_NODES") + .unwrap_or_else(|_| "127.0.0.1:9042".to_string()) + .split(',') + .map(|s| s.trim().to_string()) + .collect(); + + let scylla_keyspace = env::var("SCYLLA_KEYSPACE") + .unwrap_or_else(|_| "dyingstar".to_string()); + + let cache_flush_interval_secs = env::var("CACHE_FLUSH_INTERVAL_SECS") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(60); + + let chunk_size = env::var("CHUNK_SIZE") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(1000); + + Self { + ws_port, + rest_port, + scylla_nodes, + scylla_keyspace, + cache_flush_interval_secs, + chunk_size, + } + } +} diff --git a/persistence/src/db/connection.rs b/persistence/src/db/connection.rs new file mode 100644 index 0000000..f59487d --- /dev/null +++ b/persistence/src/db/connection.rs @@ -0,0 +1,54 @@ +use anyhow::Context; +use scylla::client::session::Session; +use scylla::client::session_builder::SessionBuilder; +use tracing::info; + +/// Build a ScyllaDB session and ensure the keyspace + table exist. +pub async fn connect(nodes: &[String], keyspace: &str) -> anyhow::Result { + info!("Connecting to ScyllaDB nodes: {:?}", nodes); + + let session = SessionBuilder::new() + .known_nodes(nodes) + .build() + .await + .context("Failed to build ScyllaDB session")?; + + info!("Connected to ScyllaDB"); + + // Create keyspace (NetworkTopologyStrategy with RF=1 for dev; + // adjust replication_factor via CQL ALTER for production clusters). + session + .query_unpaged( + format!( + "CREATE KEYSPACE IF NOT EXISTS {keyspace} \ + WITH replication = {{'class': 'NetworkTopologyStrategy', 'replication_factor': 1}}" + ), + (), + ) + .await + .context("Failed to create keyspace")?; + + // Create items table. + // `data` is stored as TEXT (JSON-serialised Vec). + // `position` and `rotation` are stored as TEXT (JSON-serialised Vec3). + session + .query_unpaged( + format!( + "CREATE TABLE IF NOT EXISTS {keyspace}.items ( \ + uuid TEXT PRIMARY KEY, \ + object_def TEXT, \ + data TEXT, \ + parent_id TEXT, \ + scenename TEXT, \ + position TEXT, \ + rotation TEXT \ + )" + ), + (), + ) + .await + .context("Failed to create items table")?; + + info!("Keyspace '{keyspace}' and table 'items' are ready"); + Ok(session) +} diff --git a/persistence/src/db/mod.rs b/persistence/src/db/mod.rs new file mode 100644 index 0000000..2d35b74 --- /dev/null +++ b/persistence/src/db/mod.rs @@ -0,0 +1,2 @@ +pub mod connection; +pub mod queries; diff --git a/persistence/src/db/queries.rs b/persistence/src/db/queries.rs new file mode 100644 index 0000000..1d8d112 --- /dev/null +++ b/persistence/src/db/queries.rs @@ -0,0 +1,211 @@ +use anyhow::Context; +use futures::StreamExt; +use scylla::client::session::Session; +use scylla::statement::prepared::PreparedStatement; +use tracing::{debug, warn}; + +use crate::websocket::messages::{Item, Vec3}; + +/// Prepared statements bundled together for reuse. +pub struct Queries { + pub upsert_item: PreparedStatement, + pub get_item: PreparedStatement, + pub delete_item: PreparedStatement, +} + +impl Queries { + pub async fn prepare(session: &Session, keyspace: &str) -> anyhow::Result { + let upsert_item = session + .prepare(format!( + "INSERT INTO {keyspace}.items \ + (uuid, object_def, data, parent_id, scenename, position, rotation) \ + VALUES (?, ?, ?, ?, ?, ?, ?)" + )) + .await + .context("Failed to prepare upsert_item statement")?; + + let get_item = session + .prepare(format!( + "SELECT uuid, object_def, data, parent_id, scenename, position, rotation \ + FROM {keyspace}.items WHERE uuid = ?" + )) + .await + .context("Failed to prepare get_item statement")?; + + let delete_item = session + .prepare(format!("DELETE FROM {keyspace}.items WHERE uuid = ?")) + .await + .context("Failed to prepare delete_item statement")?; + + Ok(Self { upsert_item, get_item, delete_item }) + } +} + +/// Stream all items from the database, collecting them into a `Vec`. +/// +/// ScyllaDB's `query_iter` handles internal paging automatically, so this +/// works for tables with millions of rows without loading them all into +/// memory in a single response. +pub async fn get_all_items(session: &Session, keyspace: &str) -> anyhow::Result> { + let mut rows_stream = session + .query_iter( + format!("SELECT uuid, object_def, data, parent_id, scenename, position, rotation FROM {keyspace}.items"), + (), + ) + .await + .context("Failed to start query_iter")? + .rows_stream::<(String, String, String, Option, Option, Option, Option)>() + .context("Failed to create typed rows stream")?; + + let mut items = Vec::new(); + while let Some(row) = rows_stream.next().await { + match row { + Ok((uuid, object_def, data_json, parent_id, scenename, position_json, rotation_json)) => { + match serde_json::from_str(&data_json) { + Ok(object_data) => { + let position: Option = + position_json.as_deref().and_then(|s| serde_json::from_str(s).ok()); + let rotation: Option = + rotation_json.as_deref().and_then(|s| serde_json::from_str(s).ok()); + items.push(Item { + uuid, + object_type: object_def, + object_data, + parent_id, + scenename, + position, + rotation, + }); + } + Err(e) => warn!("Failed to deserialise data for item {uuid}: {e}"), + } + } + Err(e) => warn!("Row deserialisation error: {e}"), + } + } + + debug!("Fetched {} items from database", items.len()); + Ok(items) +} + +/// Fetch a single item by UUID. Returns `None` if not found. +pub async fn get_item_by_uuid( + session: &Session, + queries: &Queries, + uuid: &str, +) -> anyhow::Result> { + let mut rows_stream = session + .execute_iter(queries.get_item.clone(), (uuid,)) + .await + .context("Failed to execute get_item")? + .rows_stream::<(String, String, String, Option, Option, Option, Option)>() + .context("Failed to create typed rows stream")?; + + if let Some(row) = rows_stream.next().await { + let (uuid, object_def, data_json, parent_id, scenename, position_json, rotation_json) = + row.context("Failed to deserialise get_item row")?; + let object_data = serde_json::from_str(&data_json) + .context("Failed to deserialise object_data")?; + let position: Option = + position_json.as_deref().and_then(|s| serde_json::from_str(s).ok()); + let rotation: Option = + rotation_json.as_deref().and_then(|s| serde_json::from_str(s).ok()); + Ok(Some(Item { uuid, object_type: object_def, object_data, parent_id, scenename, position, rotation })) + } else { + Ok(None) + } +} + +/// Upsert a single item to ScyllaDB. +pub async fn upsert_single_item( + session: &Session, + queries: &Queries, + item: &Item, +) -> anyhow::Result<()> { + let data_json = serde_json::to_string(&item.object_data) + .context("Failed to serialise object_data")?; + let position_json = item.position.as_ref().and_then(|v| serde_json::to_string(v).ok()); + let rotation_json = item.rotation.as_ref().and_then(|v| serde_json::to_string(v).ok()); + session + .execute_unpaged( + &queries.upsert_item, + ( + item.uuid.as_str(), + item.object_type.as_str(), + data_json, + item.parent_id.as_deref(), + item.scenename.as_deref(), + position_json, + rotation_json, + ), + ) + .await + .context("Failed to upsert item")?; + Ok(()) +} + +/// Delete an item by UUID from ScyllaDB. +pub async fn delete_item_by_uuid( + session: &Session, + queries: &Queries, + uuid: &str, +) -> anyhow::Result<()> { + session + .execute_unpaged(&queries.delete_item, (uuid,)) + .await + .context("Failed to delete item")?; + Ok(()) +} + +/// Write a batch of items to ScyllaDB using pipelined prepared statements. +/// +/// We avoid LOGGED/UNLOGGED BATCH CQL because each item has its own partition +/// key (uuid); pipelined individual inserts are more efficient in that case. +pub async fn batch_upsert_items( + session: &Session, + queries: &Queries, + items: Vec, +) -> anyhow::Result<()> { + if items.is_empty() { + return Ok(()); + } + + debug!("Flushing {} items to ScyllaDB", items.len()); + + // Fire all inserts concurrently and collect results. + let futures: Vec<_> = items + .iter() + .map(|item| { + let data_json = serde_json::to_string(&item.object_data) + .unwrap_or_else(|_| "[]".to_string()); + let position_json = item.position.as_ref() + .and_then(|v| serde_json::to_string(v).ok()); + let rotation_json = item.rotation.as_ref() + .and_then(|v| serde_json::to_string(v).ok()); + session.execute_unpaged( + &queries.upsert_item, + ( + item.uuid.as_str(), + item.object_type.as_str(), + data_json, + item.parent_id.as_deref(), + item.scenename.as_deref(), + position_json, + rotation_json, + ), + ) + }) + .collect(); + + let results = futures::future::join_all(futures).await; + + let errors: Vec<_> = results.into_iter().filter_map(|r| r.err()).collect(); + if !errors.is_empty() { + warn!("{} item(s) failed to write to ScyllaDB", errors.len()); + for e in &errors { + warn!(" write error: {e}"); + } + } + + Ok(()) +} diff --git a/persistence/src/error.rs b/persistence/src/error.rs new file mode 100644 index 0000000..9790c89 --- /dev/null +++ b/persistence/src/error.rs @@ -0,0 +1,46 @@ +use axum::{ + Json, + http::StatusCode, + response::{IntoResponse, Response}, +}; +use serde_json::json; +use thiserror::Error; + +/// Application-level errors. +/// +/// Most layers of the application return `anyhow::Result` directly for +/// convenience. This enum is only used where we need structured matching +/// (e.g. the WebSocket and REST handler layers). +#[derive(Debug, Error)] +pub enum AppError { + #[error("JSON serialization error: {0}")] + Json(#[from] serde_json::Error), + + #[error("WebSocket error: {0}")] + WebSocket(String), + + #[error("Database error: {0}")] + Database(String), + + #[error("Not found: {0}")] + NotFound(String), +} + +impl From for AppError { + fn from(e: anyhow::Error) -> Self { + AppError::Database(e.to_string()) + } +} + +impl IntoResponse for AppError { + fn into_response(self) -> Response { + let (status, message) = match &self { + AppError::NotFound(msg) => (StatusCode::NOT_FOUND, msg.clone()), + AppError::Json(e) => (StatusCode::BAD_REQUEST, e.to_string()), + AppError::Database(msg) | AppError::WebSocket(msg) => { + (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()) + } + }; + (status, Json(json!({ "error": message }))).into_response() + } +} diff --git a/persistence/src/main.rs b/persistence/src/main.rs new file mode 100644 index 0000000..d3ff8da --- /dev/null +++ b/persistence/src/main.rs @@ -0,0 +1,127 @@ +pub mod cache; +pub mod config; +pub mod db; +pub mod error; +pub mod rest; +pub mod websocket; + +use std::{net::SocketAddr, sync::Arc}; + +use tokio::{net::TcpListener, sync::broadcast, time}; +use tracing::{info, warn}; + +use scylla::client::session::Session; + +use crate::{ + cache::DualCache, + config::Config, + db::{connection::connect, queries::Queries}, + rest::build_rest_router, + websocket::server::{AppState, build_router}, +}; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + // Load .env file if present (non-fatal if absent). + let _ = dotenvy::dotenv(); + + // Initialise structured logging. + tracing_subscriber::fmt() + .with_env_filter( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| "persistence=info,tower_http=debug".parse().unwrap()), + ) + .init(); + + let config = Arc::new(Config::from_env()); + info!(?config, "Starting persistence service"); + + // Connect to ScyllaDB and prepare statements. + let session = Arc::new( + connect(&config.scylla_nodes, &config.scylla_keyspace).await?, + ); + let queries = Arc::new(Queries::prepare(&session, &config.scylla_keyspace).await?); + + // Shared dual-cache instance. + let cache = DualCache::new(); + + // Broadcast channel for pushing events from REST → WebSocket clients. + // Capacity of 256 means up to 256 unread messages per slow subscriber + // before they start being dropped (with a Lagged warning). + let (broadcaster, _) = broadcast::channel::(256); + + // Spawn the periodic flush task. + spawn_flush_task( + cache.clone(), + Arc::clone(&session), + Arc::clone(&queries), + Arc::clone(&config), + ); + + // Build and start the axum server. + let state = AppState { + session, + queries, + cache, + config: Arc::clone(&config), + broadcaster, + }; + + let ws_addr = SocketAddr::from(([0, 0, 0, 0], config.ws_port)); + let ws_listener = TcpListener::bind(ws_addr).await?; + info!("WebSocket server listening on ws://{ws_addr}/ws"); + + let rest_addr = SocketAddr::from(([0, 0, 0, 0], config.rest_port)); + let rest_listener = TcpListener::bind(rest_addr).await?; + info!("REST API server listening on http://{rest_addr}"); + + let rest_router = build_rest_router(state.clone()); + tokio::spawn(async move { + if let Err(e) = axum::serve(rest_listener, rest_router).await { + tracing::error!("REST server error: {e}"); + } + }); + + axum::serve(ws_listener, build_router(state)).await?; + + Ok(()) +} + +/// Spawn a background task that swaps the dual-cache and flushes the drained +/// items to ScyllaDB every `config.cache_flush_interval_secs` seconds. +fn spawn_flush_task( + cache: DualCache, + session: Arc, + queries: Arc, + config: Arc, +) { + let interval_secs = config.cache_flush_interval_secs; + + tokio::spawn(async move { + let mut interval = time::interval(time::Duration::from_secs(interval_secs)); + // The first tick fires immediately; skip it so we don't flush an empty + // cache right after startup. + interval.tick().await; + + loop { + interval.tick().await; + + let pending = cache.pending_count(); + if pending == 0 { + info!("Flush tick: cache empty, nothing to write"); + continue; + } + + info!("Flush tick: draining {pending} item(s) from cache"); + let items = cache.swap_and_drain(); + + if let Err(e) = + crate::db::queries::batch_upsert_items(&session, &queries, items).await + { + warn!("Cache flush error: {e}"); + } else { + info!("Flush complete: wrote {pending} item(s) to ScyllaDB"); + } + } + }); +} diff --git a/persistence/src/rest/handlers.rs b/persistence/src/rest/handlers.rs new file mode 100644 index 0000000..077e4e2 --- /dev/null +++ b/persistence/src/rest/handlers.rs @@ -0,0 +1,246 @@ +use axum::{ + Json, + extract::{Path, Query, State}, + http::StatusCode, + response::IntoResponse, +}; +use serde::Deserialize; +use serde_json::{Map, Value}; + +use crate::{ + db::queries::{delete_item_by_uuid, get_all_items, get_item_by_uuid, upsert_single_item}, + error::AppError, + websocket::{ + messages::{BridgeEventEnvelope, GenericPropsRequest, Item, PutItemRequest, Vec3}, + server::AppState, + }, +}; + +// ─── Shared helper ────────────────────────────────────────────────────────── + +/// Recompose an `Item` into the canonical GenericPropsRequest-shaped JSON: +/// `{ object_type, object_uuid, object_data: { ...rest, parent_id?, scenename?, position?, rotation? } }` +pub fn item_to_json(item: &Item) -> Value { + let mut data_map: Map = item + .object_data + .first() + .and_then(|v| v.as_object()) + .cloned() + .unwrap_or_default(); + + if let Some(ref v) = item.parent_id { + data_map.insert("parent_id".to_string(), serde_json::json!(v)); + } + if let Some(ref v) = item.scenename { + data_map.insert("scenename".to_string(), serde_json::json!(v)); + } + if let Some(ref v) = item.position { + data_map.insert("position".to_string(), serde_json::json!(v)); + } + if let Some(ref v) = item.rotation { + data_map.insert("rotation".to_string(), serde_json::json!(v)); + } + + serde_json::json!({ + "object_type": item.object_type, + "object_uuid": item.uuid, + "object_data": Value::Object(data_map), + }) +} + +// ─── Query parameters ─────────────────────────────────────────────────────── + +#[derive(Debug, Deserialize)] +pub struct GetItemsParams { + pub object_type: Option, + pub parent_id: Option, + pub scenename: Option, + #[serde(default = "default_page")] + pub page: usize, + #[serde(default = "default_page_size")] + pub page_size: usize, +} + +fn default_page() -> usize { + 1 +} +fn default_page_size() -> usize { + 100 +} + +// ─── Handlers ─────────────────────────────────────────────────────────────── + +/// POST /items +/// Create a new item. uuid is provided in the request body as `object_uuid`. +pub async fn create_item( + State(state): State, + Json(body): Json, +) -> Result { + let mut data_map = body + .object_data + .as_object() + .cloned() + .unwrap_or_default(); + + let parent_id = data_map + .remove("parent_id") + .and_then(|v| v.as_str().map(str::to_owned)); + let scenename = data_map + .remove("scenename") + .and_then(|v| v.as_str().map(str::to_owned)); + let position: Option = data_map + .remove("position") + .and_then(|v| serde_json::from_value(v).ok()); + let rotation: Option = data_map + .remove("rotation") + .and_then(|v| serde_json::from_value(v).ok()); + + let item = Item { + uuid: body.object_uuid, + object_type: body.object_type, + object_data: vec![Value::Object(data_map)], + parent_id, + scenename, + position, + rotation, + }; + + upsert_single_item(&state.session, &state.queries, &item).await?; + state.cache.insert(item.clone()); + + // Notify all active WebSocket connections. + let envelope = BridgeEventEnvelope { + event_type: "plugin".to_string(), + namespace: Some("genericprops".to_string()), + name: "create_object".to_string(), + payload: item_to_json(&item), + }; + if let Ok(text) = serde_json::to_string(&envelope) { + // Ignore send errors — no connected clients is fine. + let _ = state.broadcaster.send(text); + } + + Ok((StatusCode::CREATED, Json(item_to_json(&item)))) +} + +/// GET /items +/// Search all items with optional filters and pagination. +pub async fn get_items( + State(state): State, + Query(params): Query, +) -> Result { + let all = get_all_items(&state.session, &state.config.scylla_keyspace).await?; + + let filtered: Vec<&Item> = all + .iter() + .filter(|item| { + params + .object_type + .as_deref() + .map_or(true, |f| item.object_type == f) + && params + .parent_id + .as_deref() + .map_or(true, |f| item.parent_id.as_deref() == Some(f)) + && params + .scenename + .as_deref() + .map_or(true, |f| item.scenename.as_deref() == Some(f)) + }) + .collect(); + + let total = filtered.len(); + let page_size = params.page_size.max(1); + let page = params.page.max(1); + let skip = (page - 1) * page_size; + + let items: Vec = filtered + .into_iter() + .skip(skip) + .take(page_size) + .map(item_to_json) + .collect(); + + Ok(Json(serde_json::json!({ + "items": items, + "total": total, + "page": page, + "page_size": page_size, + }))) +} + +/// GET /items/:uuid +/// Return a single item or 404. +pub async fn get_item( + State(state): State, + Path(uuid): Path, +) -> Result { + match get_item_by_uuid(&state.session, &state.queries, &uuid).await? { + Some(item) => Ok(Json(item_to_json(&item))), + None => Err(AppError::NotFound(format!("item '{uuid}' not found"))), + } +} + +/// PUT /items/:uuid +/// Replace item data. Writes through to DB and cache. +pub async fn put_item( + State(state): State, + Path(uuid): Path, + Json(body): Json, +) -> Result { + let mut data_map = body + .object_data + .as_object() + .cloned() + .unwrap_or_default(); + + let parent_id = data_map + .remove("parent_id") + .and_then(|v| v.as_str().map(str::to_owned)); + let scenename = data_map + .remove("scenename") + .and_then(|v| v.as_str().map(str::to_owned)); + let position: Option = data_map + .remove("position") + .and_then(|v| serde_json::from_value(v).ok()); + let rotation: Option = data_map + .remove("rotation") + .and_then(|v| serde_json::from_value(v).ok()); + + let item = Item { + uuid, + object_type: body.object_type, + object_data: vec![Value::Object(data_map)], + parent_id, + scenename, + position, + rotation, + }; + + upsert_single_item(&state.session, &state.queries, &item).await?; + state.cache.insert(item.clone()); + + // Notify all active WebSocket connections. + let envelope = BridgeEventEnvelope { + event_type: "plugin".to_string(), + namespace: Some("genericprops".to_string()), + name: "update_object_from_external".to_string(), + payload: item_to_json(&item), + }; + if let Ok(text) = serde_json::to_string(&envelope) { + let _ = state.broadcaster.send(text); + } + + Ok(Json(item_to_json(&item))) +} + +/// DELETE /items/:uuid +/// Remove item from DB and cache. Always returns 204. +pub async fn delete_item( + State(state): State, + Path(uuid): Path, +) -> Result { + delete_item_by_uuid(&state.session, &state.queries, &uuid).await?; + state.cache.remove(&uuid); + Ok(StatusCode::NO_CONTENT) +} diff --git a/persistence/src/rest/mod.rs b/persistence/src/rest/mod.rs new file mode 100644 index 0000000..5d754b2 --- /dev/null +++ b/persistence/src/rest/mod.rs @@ -0,0 +1,21 @@ +pub mod handlers; + +use axum::{ + Router, + routing::{delete, get, post, put}, +}; +use tower_http::trace::TraceLayer; + +use crate::websocket::server::AppState; + +/// Build the axum router for the REST API. +pub fn build_rest_router(state: AppState) -> Router { + Router::new() + .route("/items", get(handlers::get_items)) + .route("/items", post(handlers::create_item)) + .route("/items/{uuid}", get(handlers::get_item)) + .route("/items/{uuid}", put(handlers::put_item)) + .route("/items/{uuid}", delete(handlers::delete_item)) + .layer(TraceLayer::new_for_http()) + .with_state(state) +} diff --git a/persistence/src/websocket/handlers.rs b/persistence/src/websocket/handlers.rs new file mode 100644 index 0000000..8160900 --- /dev/null +++ b/persistence/src/websocket/handlers.rs @@ -0,0 +1,245 @@ +use std::sync::Arc; + +use axum::extract::ws::{Message, WebSocket}; +use futures::{SinkExt, StreamExt}; +use scylla::client::session::Session; +use tokio::sync::broadcast; +use tracing::{debug, info, warn}; + +use serde_json::Value; + +use crate::{ + cache::DualCache, + config::Config, + db::queries::{get_all_items, Queries}, + websocket::messages::{BridgeEventEnvelope, GenericPropsRequest, Item}, +}; + +/// Handle a single WebSocket connection for its entire lifetime. +pub async fn handle_socket( + socket: WebSocket, + session: Arc, + _queries: Arc, + cache: DualCache, + config: Arc, + broadcaster: broadcast::Sender, +) { + let (mut sender, mut receiver) = socket.split(); + let mut bcast_rx = broadcaster.subscribe(); + + loop { + tokio::select! { + // ── Incoming WebSocket message ─────────────────────────────── + msg = receiver.next() => { + let msg = match msg { + Some(Ok(m)) => m, + Some(Err(e)) => { + debug!("WebSocket receive error: {e}"); + break; + } + None => break, + }; + + let text = match msg { + Message::Text(t) => t, + Message::Close(_) => break, + _ => continue, // ignore binary / ping / pong + }; + + let envelope: BridgeEventEnvelope = match serde_json::from_str(&text) { + Ok(r) => r, + Err(e) => { + let resp = BridgeEventEnvelope { + event_type: "core".to_string(), + namespace: None, + name: "error".to_string(), + payload: serde_json::json!({ "message": format!("Invalid message: {e}") }), + }; + let _ = send_json(&mut sender, &resp).await; + continue; + } + }; + + match envelope.name.as_str() { + "get_all_items" => { + if let Err(e) = handle_get_all_items( + &mut sender, + &session, + &config, + ) + .await + { + warn!("get_all_items error: {e}"); + let resp = BridgeEventEnvelope { + event_type: "core".to_string(), + namespace: None, + name: "error".to_string(), + payload: serde_json::json!({ "message": e.to_string() }), + }; + let _ = send_json(&mut sender, &resp).await; + } + } + "update_item" => { + match serde_json::from_value::(envelope.payload) { + Ok(item) => { + debug!("Caching update for item uuid={}", item.uuid); + cache.insert(item); + } + Err(e) => { + warn!("update_item: invalid payload: {e}"); + let resp = BridgeEventEnvelope { + event_type: "core".to_string(), + namespace: None, + name: "error".to_string(), + payload: serde_json::json!({ "message": format!("Invalid update_item payload: {e}") }), + }; + let _ = send_json(&mut sender, &resp).await; + } + } + } + "create_object" | "create_object_from_gameserver" => { + match serde_json::from_value::(envelope.payload) { + Ok(req) => { + let mut data_map = req.object_data + .as_object() + .cloned() + .unwrap_or_default(); + let parent_id = data_map + .remove("parent_id") + .and_then(|v| v.as_str().map(str::to_owned)); + let scenename = data_map + .remove("scenename") + .and_then(|v| v.as_str().map(str::to_owned)); + let position = data_map + .remove("position") + .and_then(|v| serde_json::from_value(v).ok()); + let rotation = data_map + .remove("rotation") + .and_then(|v| serde_json::from_value(v).ok()); + let item = Item { + object_type: req.object_type, + uuid: req.object_uuid, + parent_id, + scenename, + position, + rotation, + object_data: vec![Value::Object(data_map)], + }; + info!("Caching new object uuid={} type={}", item.uuid, item.object_type); + cache.insert(item); + } + Err(e) => { + warn!("{}: invalid payload: {e}", envelope.name); + let resp = BridgeEventEnvelope { + event_type: "core".to_string(), + namespace: None, + name: "error".to_string(), + payload: serde_json::json!({ "message": format!("Invalid {} payload: {e}", envelope.name) }), + }; + let _ = send_json(&mut sender, &resp).await; + } + } + } + other => { + warn!("Unknown event name {:?}, ignoring", other); + } + } + } + + // ── Broadcast event from REST API ──────────────────────────── + result = bcast_rx.recv() => { + match result { + Ok(text) => { + let _ = sender.send(Message::Text(text.into())).await; + } + Err(broadcast::error::RecvError::Lagged(n)) => { + warn!("WS broadcast: client lagged, {n} notification(s) dropped"); + } + Err(_) => break, + } + } + } + } + + info!("WebSocket connection closed"); +} + +/// Stream all items from the database back to the client in chunks. +async fn handle_get_all_items( + sender: &mut futures::stream::SplitSink, + session: &Session, + config: &Config, +) -> Result<(), crate::error::AppError> { + info!("Handling get_all_items request"); + + let items = get_all_items(session, &config.scylla_keyspace).await?; + let total_items = items.len(); + let chunks: Vec> = items + .chunks(config.chunk_size) + .map(|chunk| { + chunk + .iter() + .map(|item| { + let mut data_map = item + .object_data + .first() + .and_then(|v| v.as_object()) + .cloned() + .unwrap_or_default(); + if let Some(ref v) = item.parent_id { + data_map.insert("parent_id".to_string(), serde_json::json!(v)); + } + if let Some(ref v) = item.scenename { + data_map.insert("scenename".to_string(), serde_json::json!(v)); + } + if let Some(ref v) = item.position { + data_map.insert("position".to_string(), serde_json::json!(v)); + } + if let Some(ref v) = item.rotation { + data_map.insert("rotation".to_string(), serde_json::json!(v)); + } + serde_json::json!({ + "object_type": item.object_type, + "object_uuid": item.uuid, + "object_data": Value::Object(data_map), + }) + }) + .collect() + }) + .collect(); + let total_chunks = chunks.len(); + + for (index, chunk) in chunks.iter().enumerate() { + let resp = BridgeEventEnvelope { + event_type: "plugin".to_string(), + namespace: Some("genericprops".to_string()), + name: "items_chunk".to_string(), + payload: serde_json::json!({ "items": chunk, "chunk_index": index }), + }; + send_json(sender, &resp).await?; + } + + let end = BridgeEventEnvelope { + event_type: "plugin".to_string(), + namespace: Some("genericprops".to_string()), + name: "items_end".to_string(), + payload: serde_json::json!({ "total_chunks": total_chunks, "total_items": total_items }), + }; + send_json(sender, &end).await?; + + info!("Sent {total_chunks} chunk(s) to client"); + Ok(()) +} + +/// Serialise a response to JSON and send it as a WebSocket text message. +async fn send_json( + sender: &mut futures::stream::SplitSink, + value: &T, +) -> Result<(), crate::error::AppError> { + let text = serde_json::to_string(value)?; + sender + .send(Message::Text(text.into())) + .await + .map_err(|e| crate::error::AppError::WebSocket(e.to_string()))?; + Ok(()) +} diff --git a/persistence/src/websocket/messages.rs b/persistence/src/websocket/messages.rs new file mode 100644 index 0000000..b282c50 --- /dev/null +++ b/persistence/src/websocket/messages.rs @@ -0,0 +1,60 @@ +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +/// 3-D vector with decimal components. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Vec3 { + pub x: f64, + pub y: f64, + pub z: f64, +} + +/// An item stored in the persistence layer. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Item { + pub object_type: String, + pub uuid: String, + pub parent_id: Option, + pub scenename: Option, + pub position: Option, + pub rotation: Option, + /// Remaining heterogeneous object data (without the extracted fields above). + pub object_data: Vec, +} + +// ─── WebSocket request messages ──────────────────────────────────────────── + +/// Incoming request payload for create_object / create_object_from_gameserver +/// events forwarded by ds_bridge from the genericprops plugin. +#[derive(Debug, Deserialize)] +pub struct GenericPropsRequest { + pub object_type: String, + pub object_uuid: String, + pub object_data: serde_json::Value, +} + +// ─── REST request messages ───────────────────────────────────────────────── + +/// Request body for PUT /items/{uuid}. +/// The uuid comes from the URL path; this carries only the mutable fields. +#[derive(Debug, Deserialize)] +pub struct PutItemRequest { + pub object_type: String, + pub object_data: serde_json::Value, +} + +// ─── WebSocket response messages ─────────────────────────────────────────── + +/// Envelope used for all outgoing WebSocket messages, matching the +/// `BridgeEventEnvelope` format defined in `ds_bridge`. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct BridgeEventEnvelope { + /// "core" or "plugin" + pub event_type: String, + /// Plugin namespace when event_type == "plugin", otherwise None + pub namespace: Option, + /// Event name (e.g. "items_chunk", "items_end", "error") + pub name: String, + /// Arbitrary JSON payload — the event data + pub payload: serde_json::Value, +} diff --git a/persistence/src/websocket/mod.rs b/persistence/src/websocket/mod.rs new file mode 100644 index 0000000..33dcc13 --- /dev/null +++ b/persistence/src/websocket/mod.rs @@ -0,0 +1,3 @@ +pub mod handlers; +pub mod messages; +pub mod server; diff --git a/persistence/src/websocket/server.rs b/persistence/src/websocket/server.rs new file mode 100644 index 0000000..5afb510 --- /dev/null +++ b/persistence/src/websocket/server.rs @@ -0,0 +1,55 @@ +use std::sync::Arc; + +use axum::{ + Router, + extract::{State, WebSocketUpgrade, ws::WebSocket}, + response::IntoResponse, + routing::get, +}; +use scylla::client::session::Session; +use tokio::sync::broadcast; +use tower_http::trace::TraceLayer; + +use crate::{ + cache::DualCache, + config::Config, + db::queries::Queries, + websocket::handlers::handle_socket, +}; + +/// Shared application state passed to each handler. +#[derive(Clone)] +pub struct AppState { + pub session: Arc, + pub queries: Arc, + pub cache: DualCache, + pub config: Arc, + /// Broadcast channel used to push events from the REST API to all active + /// WebSocket connections. + pub broadcaster: broadcast::Sender, +} + +/// Build the axum router with the `/ws` WebSocket endpoint. +pub fn build_router(state: AppState) -> Router { + Router::new() + .route("/ws", get(ws_handler)) + .layer(TraceLayer::new_for_http()) + .with_state(state) +} + +async fn ws_handler( + upgrade: WebSocketUpgrade, + State(state): State, +) -> impl IntoResponse { + upgrade.on_upgrade(move |socket: WebSocket| async move { + handle_socket( + socket, + state.session, + state.queries, + state.cache, + state.config, + state.broadcaster, + ) + .await; + }) +}