2021-02-06 10:09:32 +01:00
|
|
|
import "missing-native-js-functions";
|
2020-11-28 19:31:04 +01:00
|
|
|
import fs from "fs/promises";
|
2021-02-14 19:01:41 +01:00
|
|
|
import { Connection } from "mongoose";
|
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";
|
2021-02-22 08:37:10 +01:00
|
|
|
import { db } from "./routes/api/v8/channels/#channelid/node_modules/fosscord-server-util";
|
2021-02-01 21:49:01 +01:00
|
|
|
import i18next from "i18next";
|
2021-02-02 00:51:00 +01:00
|
|
|
import i18nextMiddleware, { I18next } from "i18next-http-middleware";
|
|
|
|
import i18nextBackend from "i18next-node-fs-backend";
|
|
|
|
import { ErrorHandler } from "./middlewares/ErrorHandler";
|
|
|
|
import { BodyParser } from "./middlewares/BodyParser";
|
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 {
|
2021-02-02 00:51:00 +01:00
|
|
|
// @ts-ignore
|
2021-01-04 16:14:19 +01:00
|
|
|
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-02-02 00:51:00 +01:00
|
|
|
// @ts-ignore
|
|
|
|
super({ ...opts, errorHandler: false, jsonBody: false });
|
2020-11-28 19:31:04 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 19:01:41 +01:00
|
|
|
async setupSchema() {
|
|
|
|
await db.collection("users").createIndex({ id: 1 }, { unique: true });
|
|
|
|
await db.collection("messages").createIndex({ id: 1 }, { unique: true });
|
|
|
|
await db.collection("channels").createIndex({ id: 1 }, { unique: true });
|
|
|
|
await db.collection("guilds").createIndex({ id: 1 }, { unique: true });
|
2021-02-17 19:00:46 +01:00
|
|
|
await db.collection("members").createIndex({ id: 1, guild_id: 1 }, { unique: true });
|
2021-02-14 19:01:41 +01:00
|
|
|
await db.collection("roles").createIndex({ id: 1 }, { unique: true });
|
|
|
|
await db.collection("emojis").createIndex({ id: 1 }, { unique: true });
|
|
|
|
}
|
|
|
|
|
2021-01-04 16:14:19 +01:00
|
|
|
async start() {
|
2021-02-14 19:01:41 +01:00
|
|
|
// @ts-ignore
|
2021-02-16 21:20:07 +01:00
|
|
|
await (db as Promise<Connection>);
|
2021-02-14 19:01:41 +01:00
|
|
|
await this.setupSchema();
|
2021-01-30 19:58:15 +01:00
|
|
|
console.log("[DB] connected");
|
|
|
|
await Promise.all([Config.init()]);
|
|
|
|
|
|
|
|
this.app.use(GlobalRateLimit);
|
|
|
|
this.app.use(Authentication);
|
2021-02-02 00:51:00 +01:00
|
|
|
this.app.use(BodyParser({ inflate: true }));
|
|
|
|
const languages = await fs.readdir(__dirname + "/../locales/");
|
|
|
|
const namespaces = await fs.readdir(__dirname + "/../locales/en/");
|
2021-02-01 21:49:01 +01:00
|
|
|
const ns = namespaces.filter((x) => x.endsWith(".json")).map((x) => x.slice(0, x.length - 5));
|
|
|
|
|
2021-02-02 00:51:00 +01:00
|
|
|
await i18next
|
|
|
|
.use(i18nextBackend)
|
|
|
|
.use(i18nextMiddleware.LanguageDetector)
|
|
|
|
.init({
|
|
|
|
preload: languages,
|
|
|
|
// debug: true,
|
|
|
|
fallbackLng: "en",
|
|
|
|
ns,
|
|
|
|
backend: {
|
|
|
|
loadPath: __dirname + "/../locales/{{lng}}/{{ns}}.json",
|
|
|
|
},
|
|
|
|
load: "all",
|
|
|
|
});
|
2021-02-01 21:49:01 +01:00
|
|
|
this.app.use(i18nextMiddleware.handle(i18next, {}));
|
2021-01-30 19:58:15 +01:00
|
|
|
|
2020-11-28 19:31:04 +01:00
|
|
|
this.routes = await this.registerRoutes(__dirname + "/routes/");
|
2021-02-02 00:51:00 +01:00
|
|
|
this.app.use(ErrorHandler);
|
2021-02-01 21:49:01 +01:00
|
|
|
const indexHTML = await fs.readFile(__dirname + "/../client_test/index.html");
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-02-22 08:21:32 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|