Compare commits
No commits in common. "7a28a7e31401e4f9aa574c07b316d339aab09ab6" and "07263baf6a9c2e96416e229b4f17d44a85a2daf0" have entirely different histories.
7a28a7e314
...
07263baf6a
4 changed files with 3 additions and 190 deletions
31
package-lock.json
generated
31
package-lock.json
generated
|
@ -2660,14 +2660,6 @@
|
|||
"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",
|
||||
|
@ -7246,26 +7238,6 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"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",
|
||||
|
@ -9720,8 +9692,7 @@
|
|||
"name": "connect-force-server",
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"randexp": "^0.5.3"
|
||||
"express": "^4.18.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.15",
|
||||
|
|
|
@ -8,8 +8,7 @@
|
|||
"start": "noderel -e ./src/main.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"randexp": "^0.5.3"
|
||||
"express": "^4.18.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.15",
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
/**
|
||||
* Represents an http error.
|
||||
*/
|
||||
export class HTTPError extends Error
|
||||
{
|
||||
/**
|
||||
* The http status code.
|
||||
*
|
||||
* @type {number}
|
||||
*/
|
||||
status;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the {@link HTTPError `HTTPError`} class.
|
||||
*
|
||||
* @param {number} status
|
||||
* The http status code.
|
||||
*
|
||||
* @param {string} message
|
||||
* The error message.
|
||||
*/
|
||||
constructor(status, message)
|
||||
{
|
||||
super(message);
|
||||
this.status = status;
|
||||
}
|
||||
}
|
|
@ -1,149 +1,19 @@
|
|||
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();
|
||||
|
||||
const apiKeys = [
|
||||
"c4game"
|
||||
];
|
||||
|
||||
const dataPath = "/api/data";
|
||||
const parametrizedDataPath = "/api/data/:id";
|
||||
|
||||
/**
|
||||
* The data provided by the api.
|
||||
*
|
||||
* @type {Record<string, unknown>}
|
||||
*/
|
||||
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());
|
||||
|
||||
app.use(
|
||||
"/api",
|
||||
(request, response, next) =>
|
||||
{
|
||||
const keyParam = "token";
|
||||
|
||||
if (keyParam in request.query)
|
||||
{
|
||||
let key = request.query[keyParam];
|
||||
|
||||
if (typeof key === "string" && apiKeys.includes(key))
|
||||
{
|
||||
next();
|
||||
}
|
||||
else
|
||||
{
|
||||
next(new HTTPError(401, "The specified API token is invalid"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
next(new HTTPError(401, "An API token is required"));
|
||||
}
|
||||
});
|
||||
|
||||
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) =>
|
||||
{
|
||||
response.send(`${error}`);
|
||||
response.status(error instanceof HTTPError ? error.status : 500);
|
||||
response.status(error?.status ?? 500);
|
||||
}
|
||||
]);
|
||||
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue