46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import * as env from "./env.js";
|
|
import express from "express";
|
|
import { promises as fs } from "node:fs";
|
|
import path from "path";
|
|
import { ensureDatabases } from "./auth.js";
|
|
import RootRouter from "./root_router.js";
|
|
import HostRouter from "./host_router.js";
|
|
import cookieParser from "cookie-parser";
|
|
import { gameExists } from "./game.js";
|
|
|
|
|
|
console.log("[Checking structure]");
|
|
await fs.mkdir(path.join(env.DataRoot, "storage"), { recursive: true });
|
|
await fs.mkdir(path.join(env.DataRoot, "games"), { recursive: true });
|
|
await ensureDatabases();
|
|
|
|
const app = express();
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.use((req, res, next) => {
|
|
let host = req.headers["host"];
|
|
if (!host) {
|
|
return next();
|
|
}
|
|
|
|
if (host === env.RootDomain) {
|
|
return RootRouter(req, res, next);
|
|
}
|
|
|
|
if (host.endsWith("." + env.RootDomain)) {
|
|
const game = host.split("." + env.RootDomain)[0];
|
|
if (gameExists(game)) {
|
|
req.game = game;
|
|
return HostRouter(req, res, next);
|
|
}
|
|
}
|
|
|
|
return next();
|
|
});
|
|
|
|
app.get("/", (req, res) => {
|
|
res.status(400).send("Invalid host.");
|
|
});
|
|
|
|
app.listen(env.Port); |