2020-11-28 19:31:04 +01:00
|
|
|
import fs from "fs/promises";
|
2021-01-04 16:14:19 +01:00
|
|
|
import { Server, ServerOptions } from "lambert-server";
|
2021-01-30 19:58:15 +01:00
|
|
|
import { Authentication, GlobalRateLimit } from "./middlewares/";
|
|
|
|
import Config from "./util/Config";
|
|
|
|
import db from "./util/Database";
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-01-04 16:14:19 +01:00
|
|
|
export interface DiscordServerOptions extends ServerOptions {}
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-01-04 16:14:19 +01:00
|
|
|
declare global {
|
|
|
|
namespace Express {
|
|
|
|
interface Request {
|
|
|
|
server: DiscordServer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-01-04 16:14:19 +01:00
|
|
|
export class DiscordServer extends Server {
|
|
|
|
public options: DiscordServerOptions;
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-01-04 16:29:49 +01:00
|
|
|
constructor(opts?: Partial<DiscordServerOptions>) {
|
2021-01-04 16:14:19 +01:00
|
|
|
super(opts);
|
2020-11-28 19:31:04 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 16:14:19 +01:00
|
|
|
async start() {
|
2021-01-30 19:58:15 +01:00
|
|
|
await db.init();
|
|
|
|
console.log("[DB] connected");
|
|
|
|
await Promise.all([Config.init()]);
|
|
|
|
|
|
|
|
this.app.use(GlobalRateLimit);
|
|
|
|
this.app.use(Authentication);
|
|
|
|
|
2020-11-28 19:31:04 +01:00
|
|
|
// recursively loads files in routes/
|
|
|
|
this.routes = await this.registerRoutes(__dirname + "/routes/");
|
|
|
|
// const indexHTML = await (await fetch("https://discord.com/app")).buffer();
|
|
|
|
const indexHTML = await fs.readFile(__dirname + "/../client/index.html");
|
|
|
|
|
|
|
|
this.app.get("*", (req, res) => {
|
|
|
|
res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
|
|
|
|
res.set("content-type", "text/html");
|
|
|
|
res.send(indexHTML);
|
|
|
|
});
|
2021-01-04 16:14:19 +01:00
|
|
|
return super.start();
|
2020-11-28 19:31:04 +01:00
|
|
|
}
|
|
|
|
}
|