Implement backend and frontend loading

This system lets a server modularly load backend and frontend and allows
hosting each of the parts on separate services.
This commit is contained in:
Wolvan 2021-12-28 23:47:30 +01:00
parent 8bc3f7fe36
commit 1031a4c36f
5 changed files with 1243 additions and 10 deletions

1198
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -34,6 +34,8 @@
"devDependencies": {
"@types/chai": "^4.2.22",
"@types/chai-as-promised": "^7.1.4",
"@types/compression": "^1.7.2",
"@types/express": "^4.17.13",
"@types/fs-extra": "^9.0.13",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.10",
@ -52,6 +54,8 @@
"typescript": "^4.5.2"
},
"dependencies": {
"commander": "^8.3.0"
"commander": "^8.3.0",
"compression": "^1.7.4",
"express": "^4.17.2"
}
}

7
src/backend.ts Normal file
View file

@ -0,0 +1,7 @@
"use strict";
import { Router } from "express";
export default function init(router: Router): void {
}

6
src/frontend.ts Normal file
View file

@ -0,0 +1,6 @@
"use strict";
import { Router } from "express";
export default function init(router: Router): void {
}

View file

@ -1,13 +1,47 @@
#!/usr/bin/env node
"use strict";
import loadConfig from "./config-loader";
import { program } from "commander";
import express from "express";
import compression from "compression";
import { resolve } from "path";
async function main(): Promise<void> {
await loadConfig([
["--no-frontend", "Do not start the frontend server"],
["--no-backend", "Do not start the backend server"],
["-p, --port <port>", "Port to listen on", (port: any) => parseInt(port), 6969],
["--backend-base-url <url>", "Base URL for the backend server", null],
], ".poll-horse-config");
const opts = program.opts();
const app = express();
app.use(express.json());
app.use(compression());
if (opts.backend) {
console.log(`Mounting backend`);
const backendRouter = express.Router();
const backend = await import("./backend");
await backend.default(backendRouter);
app.use("/_backend/", backendRouter);
}
if (opts.frontend) {
console.log(`Mounting frontend`);
const frontendRouter = express.Router();
const frontend = await import("./frontend");
await frontend.default(frontendRouter);
app.use("/static", express.static(resolve(__dirname, "../frontend/static")));
app.use("/", frontendRouter);
}
app.listen(opts.port, () => {
console.log(`Listening on port ${opts.port}`);
});
}
main();