summary refs log tree commit diff
path: root/src/Server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/Server.ts')
-rw-r--r--src/Server.ts46
1 files changed, 26 insertions, 20 deletions
diff --git a/src/Server.ts b/src/Server.ts

index f17d4c9d..9c3ea9d6 100644 --- a/src/Server.ts +++ b/src/Server.ts
@@ -3,16 +3,16 @@ import fs from "fs/promises"; import { Connection } from "mongoose"; import { Server, ServerOptions } from "lambert-server"; import { Authentication, CORS, GlobalRateLimit } from "./middlewares/"; -import Config from "./util/Config"; -import { db } from "@fosscord/server-util"; +import { Config, db } from "@fosscord/server-util"; import i18next from "i18next"; import i18nextMiddleware, { I18next } from "i18next-http-middleware"; import i18nextBackend from "i18next-node-fs-backend"; import { ErrorHandler } from "./middlewares/ErrorHandler"; import { BodyParser } from "./middlewares/BodyParser"; -import { Router } from "express"; +import express, { Router } from "express"; import fetch from "node-fetch"; import mongoose from "mongoose"; +import path from "path"; // this will return the new updated document for findOneAndUpdate mongoose.set("returnOriginal", false); // https://mongoosejs.com/docs/api/model.html#model_Model.findOneAndUpdate @@ -37,13 +37,17 @@ export class FosscordServer extends Server { } 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 }); - await db.collection("members").createIndex({ id: 1, guild_id: 1 }, { unique: true }); - await db.collection("roles").createIndex({ id: 1 }, { unique: true }); - await db.collection("emojis").createIndex({ id: 1 }, { unique: true }); + return Promise.all([ + db.collection("users").createIndex({ id: 1 }, { unique: true }), + db.collection("messages").createIndex({ id: 1 }, { unique: true }), + db.collection("channels").createIndex({ id: 1 }, { unique: true }), + db.collection("guilds").createIndex({ id: 1 }, { unique: true }), + db.collection("members").createIndex({ id: 1, guild_id: 1 }, { unique: true }), + db.collection("roles").createIndex({ id: 1 }, { unique: true }), + db.collection("emojis").createIndex({ id: 1 }, { unique: true }), + db.collection("invites").createIndex({ code: 1 }, { unique: true }), + db.collection("invites").createIndex({ expires_at: 1 }, { expireAfterSeconds: 0 }) // after 0 seconds of expires_at the invite will get delete + ]); } async start() { @@ -51,14 +55,14 @@ export class FosscordServer extends Server { await (db as Promise<Connection>); await this.setupSchema(); console.log("[DB] connected"); - await Promise.all([Config.init()]); + await Config.init(); this.app.use(GlobalRateLimit); this.app.use(Authentication); this.app.use(CORS); this.app.use(BodyParser({ inflate: true })); - const languages = await fs.readdir(__dirname + "/../locales/"); - const namespaces = await fs.readdir(__dirname + "/../locales/en/"); + const languages = await fs.readdir(path.join(__dirname, "..", "locales")); + const namespaces = await fs.readdir(path.join(__dirname, "..", "locales", "en")); const ns = namespaces.filter((x) => x.endsWith(".json")).map((x) => x.slice(0, x.length - 5)); await i18next @@ -70,9 +74,9 @@ export class FosscordServer extends Server { fallbackLng: "en", ns, backend: { - loadPath: __dirname + "/../locales/{{lng}}/{{ns}}.json", + loadPath: __dirname + "/../locales/{{lng}}/{{ns}}.json" }, - load: "all", + load: "all" }); this.app.use(i18nextMiddleware.handle(i18next, {})); @@ -81,19 +85,21 @@ export class FosscordServer extends Server { // @ts-ignore this.app = prefix; - this.routes = await this.registerRoutes(__dirname + "/routes/"); + this.routes = await this.registerRoutes(path.join(__dirname, "routes")); app.use("/api/v8", prefix); this.app = app; this.app.use(ErrorHandler); - const indexHTML = await fs.readFile(__dirname + "/../client_test/index.html"); + const indexHTML = await fs.readFile(path.join(__dirname, "..", "client_test", "index.html")); + + this.app.use("/assets", express.static(path.join(__dirname, "..", "assets"))); this.app.get("/assets/:file", async (req, res) => { delete req.headers.host; const response = await fetch(`https://discord.com/assets/${req.params.file}`, { // @ts-ignore headers: { - ...req.headers, - }, + ...req.headers + } }); const buffer = await response.buffer(); @@ -107,7 +113,7 @@ export class FosscordServer extends Server { "transfer-encoding", "expect-ct", "access-control-allow-origin", - "content-encoding", + "content-encoding" ].includes(name.toLowerCase()) ) { return;