Remove express server

This commit is contained in:
Manuel Thalmann 2022-12-15 00:53:33 +01:00
parent d87da24d30
commit 6df42d2e3f
No known key found for this signature in database
GPG key ID: 5FD9AD3CCDDBD27B
11 changed files with 1 additions and 440 deletions

View file

@ -4,10 +4,6 @@
"name": "ConnectForce",
"path": "./packages/game"
},
{
"name": "Server",
"path": "./packages/server"
},
{
"name": "Solution Items",
"path": "."
@ -117,29 +113,7 @@
"preLaunchTask": "Build",
"pathMapping": {
"/": "${workspaceFolder:ConnectForce}/src"
},
"presentation": {
"hidden": true
}
},
{
"type": "node",
"request": "launch",
"name": "Launch Server",
"program": "${workspaceFolder:Server}/src/main.js",
"console": "integratedTerminal",
"presentation": {
"hidden": true
}
}
],
"compounds": [
{
"name": "Launch Project in Chrome",
"configurations": [
"Launch Website in Chrome",
"Launch Server"
]
}
]
},

View file

@ -137,7 +137,7 @@ export let Watch: TaskFunction = async (): Promise<void> =>
syncer.init({
open: false,
proxy: "http://localhost:1337",
server: join(context.StaticPath()),
online: false
});

View file

@ -1,13 +0,0 @@
const { join } = require("node:path");
module.exports = {
env: {
browser: true
},
parserOptions: {
project: [
join(__dirname, "app.jsconfig.json"),
join(__dirname, "eslint.jsconfig.json")
]
}
};

View file

@ -1,168 +0,0 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# Source-files
[Ss]rc/
# TypeScript config-files
tsconfig.json
tsconfig.*.json
# Lint config-files
.eslintrc
.eslintrc.*
# Source-maps
[Ll]ib/**/*.map
# Unit-Tests
.mocharc.*
[Ll]ib/tests/
# Visual Studio Code-Environment
.vscode/
# GitHub configuration
.github/
# CI configuration
.drone.yml
.woodpecker.yml
# Build Environment
gulp/
gulpfile.ts
# Temporary release-assets
.tagName.txt
.tagHeading.txt
.releaseNotes.md
.releaseTitle.md

View file

@ -1,11 +0,0 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"composite": true
},
"include": [
"./src/**/*"
]
}

View file

@ -1,10 +0,0 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true
},
"include": [
"./.eslintrc.cjs"
]
}

View file

@ -1,19 +0,0 @@
{
"name": "connect-force-server",
"version": "0.0.0",
"type": "module",
"description": "A server for Storing ConnectForce Savegames",
"author": "Manuel Thalmann <m@nuth.ch>",
"scripts": {
"start": "noderel -e ./src/main.js"
},
"dependencies": {
"express": "^4.18.2",
"randexp": "^0.5.3"
},
"devDependencies": {
"@types/express": "^4.17.15",
"@types/node": "^18.11.15",
"noderel": "^1.0.13"
}
}

View file

@ -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;
}
}

View file

@ -1,150 +0,0 @@
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}){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;
response.send(data[id]);
}
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);
}
]);
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);

View file

@ -1,3 +0,0 @@
{
"extends": "../../tsconfig.base.json"
}

View file

@ -1,12 +0,0 @@
{
"extends": "./tsconfig.base.json",
"references": [
{
"path": "./app.jsconfig.json"
},
{
"path": "./eslint.jsconfig.json"
}
],
"include": []
}