1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-13 14:12:41 +01:00
server/api/src/Server.ts

89 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-02-06 10:09:32 +01:00
import "missing-native-js-functions";
2021-02-14 19:01:41 +01:00
import { Connection } from "mongoose";
import { Server, ServerOptions } from "lambert-server";
2021-06-27 23:14:13 +02:00
import { Authentication, CORS } from "./middlewares/";
2021-08-13 20:53:50 +02:00
import { Config, db, initEvent } from "@fosscord/util";
2021-02-02 00:51:00 +01:00
import { ErrorHandler } from "./middlewares/ErrorHandler";
import { BodyParser } from "./middlewares/BodyParser";
2021-08-13 20:53:50 +02:00
import { Router, Request, Response } from "express";
2021-04-26 00:10:20 +02:00
import mongoose from "mongoose";
2021-05-24 20:47:06 +02:00
import path from "path";
import { initRateLimits } from "./middlewares/RateLimit";
import TestClient from "./middlewares/TestClient";
2021-08-13 20:53:50 +02:00
import { initTranslation } from "./middlewares/Translation";
2020-11-28 19:31:04 +01:00
2021-04-26 00:10:20 +02:00
// this will return the new updated document for findOneAndUpdate
mongoose.set("returnOriginal", false); // https://mongoosejs.com/docs/api/model.html#model_Model.findOneAndUpdate
2021-04-26 00:10:20 +02:00
export interface FosscordServerOptions extends ServerOptions {}
2020-11-28 19:31:04 +01:00
declare global {
namespace Express {
interface Request {
2021-02-02 00:51:00 +01:00
// @ts-ignore
2021-04-22 23:29:06 +02:00
server: FosscordServer;
}
}
}
2020-11-28 19:31:04 +01:00
2021-04-22 23:29:06 +02:00
export class FosscordServer extends Server {
public declare options: FosscordServerOptions;
2020-11-28 19:31:04 +01:00
2021-04-22 23:29:06 +02:00
constructor(opts?: Partial<FosscordServerOptions>) {
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() {
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 }),
2021-07-01 09:33:54 +02:00
db.collection("invites").createIndex({ expires_at: 1 }, { expireAfterSeconds: 0 }), // after 0 seconds of expires_at the invite will get delete
2021-07-01 11:16:13 +02:00
db.collection("ratelimits").createIndex({ expires_at: 1 }, { expireAfterSeconds: 0 })
]);
2021-02-14 19:01:41 +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-05-24 20:47:06 +02:00
await Config.init();
await initEvent();
2021-01-30 19:58:15 +01:00
this.app.use(CORS);
this.app.use(BodyParser({ inflate: true, limit: 1024 * 1024 * 10 })); // 2MB
2021-01-30 19:58:15 +01:00
2021-04-05 21:43:11 +02:00
const app = this.app;
2021-08-13 20:53:50 +02:00
const api = Router(); // @ts-ignore
this.app = api;
2021-04-05 21:43:11 +02:00
2021-08-13 20:53:50 +02:00
api.use(Authentication);
initRateLimits(api);
2021-08-13 20:53:50 +02:00
await initTranslation(api);
2021-05-24 20:57:22 +02:00
this.routes = await this.registerRoutes(path.join(__dirname, "routes", "/"));
2021-08-13 20:53:50 +02:00
api.use("*", (req: Request, res: Response) => {
res.status(404).json({
message: "404: Not Found",
code: 0
});
});
2021-04-05 21:43:11 +02:00
this.app = app;
2021-08-13 20:53:50 +02:00
app.use("/api/v8", api);
app.use("/api/v9", api);
app.use("/api", api); // allow unversioned requests
2021-02-02 00:51:00 +01:00
this.app.use(ErrorHandler);
TestClient(this.app);
2021-04-05 21:43:11 +02:00
return super.start();
2020-11-28 19:31:04 +01:00
}
}