From 7a28a7e31401e4f9aa574c07b316d339aab09ab6 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 14 Dec 2022 11:23:04 +0100 Subject: [PATCH] Add express handlers for managing save games --- package-lock.json | 31 ++++++++++- packages/server/package.json | 3 +- packages/server/src/main.js | 99 ++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 45bcac4..21e89bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2660,6 +2660,14 @@ "node": ">=6.0.0" } }, + "node_modules/drange": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/drange/-/drange-1.1.1.tgz", + "integrity": "sha512-pYxfDYpued//QpnLIm4Avk7rsNtAtQkUES2cwAYSvD/wd2pKD71gN2Ebj3e7klzXwjocvE8c5vx/1fxwpqmSxA==", + "engines": { + "node": ">=4" + } + }, "node_modules/duplexify": { "version": "3.7.1", "resolved": "https://registry.npmjs.org/duplexify/-/duplexify-3.7.1.tgz", @@ -7238,6 +7246,26 @@ } ] }, + "node_modules/randexp": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/randexp/-/randexp-0.5.3.tgz", + "integrity": "sha512-U+5l2KrcMNOUPYvazA3h5ekF80FHTUG+87SEAmHZmolh1M+i/WyTCxVzmi+tidIa1tM4BSe8g2Y/D3loWDjj+w==", + "dependencies": { + "drange": "^1.0.2", + "ret": "^0.2.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/randexp/node_modules/ret": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/ret/-/ret-0.2.2.tgz", + "integrity": "sha512-M0b3YWQs7R3Z917WRQy1HHA7Ba7D8hvZg6UE5mLykJxQVE2ju0IXbGlaHPPlkY+WN7wFP+wUMXmBFA0aV6vYGQ==", + "engines": { + "node": ">=4" + } + }, "node_modules/range-parser": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", @@ -9692,7 +9720,8 @@ "name": "connect-force-server", "version": "0.0.0", "dependencies": { - "express": "^4.18.2" + "express": "^4.18.2", + "randexp": "^0.5.3" }, "devDependencies": { "@types/express": "^4.17.15", diff --git a/packages/server/package.json b/packages/server/package.json index 07413bf..4d61558 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -8,7 +8,8 @@ "start": "noderel -e ./src/main.js" }, "dependencies": { - "express": "^4.18.2" + "express": "^4.18.2", + "randexp": "^0.5.3" }, "devDependencies": { "@types/express": "^4.17.15", diff --git a/packages/server/src/main.js b/packages/server/src/main.js index 8a767e4..aa8b964 100644 --- a/packages/server/src/main.js +++ b/packages/server/src/main.js @@ -1,8 +1,11 @@ import { join } from "path"; import { fileURLToPath } from "url"; import express from "express"; +import RandExp from "randexp"; import { HTTPError } from "./HTTPError.js"; +const { randexp } = RandExp; + const dirname = fileURLToPath(new URL(".", import.meta.url)); const app = express(); @@ -10,6 +13,27 @@ const apiKeys = [ "c4game" ]; +const dataPath = "/api/data"; +const parametrizedDataPath = "/api/data/:id"; + +/** + * The data provided by the api. + * + * @type {Record} + */ +let data = {}; + +/** + * Creates a new guid. + * + * @returns {string} + * The newly created guid. + */ +function createGuid() +{ + return randexp(/[0-9a-f]{8}(-[0-9a-f]){4}[0-9a-f]{8}/); +} + app.use(express.static(join(dirname, "..", "..", "game", "lib", "static"))); app.use("/api", express.json()); @@ -38,6 +62,66 @@ app.use( } }); +app.get( + parametrizedDataPath, + (request, response, next) => + { + let id = request.params.id; + console.log(`Data ID \`${id}\` requested`); + + if (id in data) + { + response.send(data[id]); + } + else + { + next(); + } + }); + +app.post( + dataPath, + (request, response, next) => + { + let id = createGuid(); + data[id] = request.body; + response.send({ id }); + }); + +app.put( + parametrizedDataPath, + (request, response, next) => + { + let id = request.params.id; + + if (id in data) + { + data[id] = request.body; + } + else + { + next(); + } + }); + +app.delete( + parametrizedDataPath, + (request, response, next) => + { + let id = request.params.id; + + if (id in data) + { + delete data[id]; + response.send(); + response.status(204); + } + else + { + next(); + } + }); + app.use( [ (error, request, response, next) => @@ -47,4 +131,19 @@ app.use( } ]); +app.use( + "/api", + (request, response) => + { + response.send({ error: "Not Found" }); + response.status(404); + }); + +app.use( + (request, response) => + { + response.send("Not Found"); + response.status(404); + }); + app.listen(1337);