2021-09-18 20:43:00 +02:00
|
|
|
import { OptionsJson } from 'body-parser';
|
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";
|
2021-01-04 16:14:19 +01:00
|
|
|
import { Server, ServerOptions } from "lambert-server";
|
2021-06-27 23:14:13 +02:00
|
|
|
import { Authentication, CORS } from "./middlewares/";
|
2021-08-29 00:03:40 +02:00
|
|
|
import { Config, initDatabase, initEvent } from "@fosscord/util";
|
2021-02-02 00:51:00 +01:00
|
|
|
import { ErrorHandler } from "./middlewares/ErrorHandler";
|
|
|
|
import { BodyParser } from "./middlewares/BodyParser";
|
2021-08-17 20:37:13 +02:00
|
|
|
import { Router, Request, Response, NextFunction } 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";
|
2021-08-12 18:57:25 +02:00
|
|
|
import { initRateLimits } from "./middlewares/RateLimit";
|
2021-08-09 12:58:05 +02:00
|
|
|
import TestClient from "./middlewares/TestClient";
|
2021-08-13 20:53:50 +02:00
|
|
|
import { initTranslation } from "./middlewares/Translation";
|
2021-09-18 18:13:15 +02:00
|
|
|
import morgan from "morgan";
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-05-01 13:54:12 +02:00
|
|
|
export interface FosscordServerOptions 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-04-22 23:29:06 +02:00
|
|
|
server: FosscordServer;
|
2021-01-04 16:14:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-04-22 23:29:06 +02:00
|
|
|
export class FosscordServer extends Server {
|
2021-05-31 21:03:09 +02:00
|
|
|
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-01-04 16:14:19 +01:00
|
|
|
async start() {
|
2021-08-29 00:03:40 +02:00
|
|
|
await initDatabase();
|
2021-05-24 20:47:06 +02:00
|
|
|
await Config.init();
|
2021-08-13 12:59:59 +02:00
|
|
|
await initEvent();
|
2021-01-30 19:58:15 +01:00
|
|
|
|
2021-09-18 20:43:00 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
DOCUMENTATION: uses log-requests environment variable
|
|
|
|
|
|
|
|
# only log 200 and 204
|
|
|
|
log-requests=200 204
|
|
|
|
# log everything except 200 and 204
|
|
|
|
log-requests=-200 204
|
|
|
|
# log all requests
|
|
|
|
log-requests=-
|
|
|
|
*/
|
|
|
|
|
2021-09-18 18:13:15 +02:00
|
|
|
let logRequests = process.env["log-requests"] != undefined;
|
|
|
|
if(logRequests) {
|
2021-09-18 20:43:00 +02:00
|
|
|
this.app.use(morgan("combined", {
|
|
|
|
skip: (req, res) => {
|
|
|
|
var skip = !(process.env["log-requests"]?.includes(res.statusCode.toString()) ?? false);
|
|
|
|
if(process.env["log-requests"]?.charAt(0) == '-') skip = !skip;
|
|
|
|
return skip;
|
|
|
|
}
|
|
|
|
}));
|
2021-09-18 18:13:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-01 13:54:12 +02:00
|
|
|
this.app.use(CORS);
|
2021-09-08 13:34:37 +02:00
|
|
|
this.app.use(BodyParser({ inflate: true, limit: "10mb" }));
|
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
|
2021-08-09 12:58:05 +02:00
|
|
|
this.app = api;
|
2021-04-05 21:43:11 +02:00
|
|
|
|
2021-08-13 20:53:50 +02:00
|
|
|
api.use(Authentication);
|
2021-08-16 15:06:31 +02:00
|
|
|
await 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-07-22 14:04:24 +02:00
|
|
|
|
2021-08-17 20:37:13 +02:00
|
|
|
api.use("*", (error: any, req: Request, res: Response, next: NextFunction) => {
|
|
|
|
if (error) return next(error);
|
2021-07-22 14:04:24 +02:00
|
|
|
res.status(404).json({
|
|
|
|
message: "404: Not Found",
|
|
|
|
code: 0
|
|
|
|
});
|
2021-08-15 21:56:30 +02:00
|
|
|
next();
|
2021-07-22 14:04:24 +02:00
|
|
|
});
|
|
|
|
|
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);
|
2021-08-09 12:58:05 +02:00
|
|
|
TestClient(this.app);
|
2021-04-05 21:43:11 +02:00
|
|
|
|
2021-09-18 18:13:15 +02:00
|
|
|
if(logRequests){
|
|
|
|
console.log("Warning: Request logging is enabled! This will spam your console!\nTo disable this, unset the 'log-requests' environment variable!");
|
|
|
|
}
|
2021-01-04 16:14:19 +01:00
|
|
|
return super.start();
|
2020-11-28 19:31:04 +01:00
|
|
|
}
|
|
|
|
}
|