2020-05-23 16:22:03 +03:00
|
|
|
import cors from "cors";
|
2021-06-06 23:51:32 +02:00
|
|
|
import express from "express";
|
2023-04-01 12:58:17 +01:00
|
|
|
import multer from "multer";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { TokenError } from "passport-oauth2";
|
2023-04-01 12:58:17 +01:00
|
|
|
import { env } from "../env";
|
2020-05-23 16:22:03 +03:00
|
|
|
import { initArchives } from "./archives";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { initAuth } from "./auth";
|
2020-05-23 16:22:03 +03:00
|
|
|
import { initDocs } from "./docs";
|
2021-11-03 00:05:53 +02:00
|
|
|
import { initGuildsAPI } from "./guilds/index";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { clientError, error, notFound } from "./responses";
|
2021-09-05 13:53:46 +03:00
|
|
|
import { startBackgroundTasks } from "./tasks";
|
2020-05-23 16:22:03 +03:00
|
|
|
|
2024-04-06 11:54:31 +00:00
|
|
|
const apiPathPrefix = env.HOST_MODE === "lightweight" ? env.LIGHTWEIGHT_API_PATH_PREFIX || "" : "/api";
|
|
|
|
|
2020-05-23 16:22:03 +03:00
|
|
|
const app = express();
|
|
|
|
|
|
|
|
app.use(
|
|
|
|
cors({
|
2022-06-26 19:30:46 +03:00
|
|
|
origin: env.DASHBOARD_URL,
|
2020-05-23 16:22:03 +03:00
|
|
|
}),
|
|
|
|
);
|
2021-08-20 21:37:25 +03:00
|
|
|
app.use(
|
|
|
|
express.json({
|
2021-11-03 01:16:29 +02:00
|
|
|
limit: "50mb",
|
2021-08-20 21:37:25 +03:00
|
|
|
}),
|
|
|
|
);
|
2021-11-03 01:14:41 +02:00
|
|
|
app.use(multer().none());
|
2020-05-23 16:22:03 +03:00
|
|
|
|
2024-04-06 11:54:31 +00:00
|
|
|
const rootRouter = express.Router();
|
|
|
|
|
2020-05-23 16:22:03 +03:00
|
|
|
initAuth(app);
|
|
|
|
initGuildsAPI(app);
|
|
|
|
initArchives(app);
|
|
|
|
initDocs(app);
|
|
|
|
|
|
|
|
// Default route
|
2024-04-06 11:54:31 +00:00
|
|
|
rootRouter.get("/", (req, res) => {
|
2020-05-23 16:22:03 +03:00
|
|
|
res.json({ status: "cookies", with: "milk" });
|
|
|
|
});
|
|
|
|
|
2024-04-06 11:54:31 +00:00
|
|
|
app.use(apiPathPrefix, rootRouter);
|
|
|
|
|
2020-05-23 16:22:03 +03:00
|
|
|
// Error response
|
2023-05-08 22:58:51 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2020-05-23 16:22:03 +03:00
|
|
|
app.use((err, req, res, next) => {
|
|
|
|
if (err instanceof TokenError) {
|
|
|
|
clientError(res, "Invalid code");
|
|
|
|
} else {
|
|
|
|
console.error(err); // tslint:disable-line
|
|
|
|
error(res, "Server error", err.status || 500);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 404 response
|
2023-05-08 22:58:51 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2020-05-23 16:22:03 +03:00
|
|
|
app.use((req, res, next) => {
|
|
|
|
return notFound(res);
|
|
|
|
});
|
|
|
|
|
2024-03-17 18:49:31 +02:00
|
|
|
const port = 3001;
|
2020-07-25 12:39:37 +03:00
|
|
|
app.listen(port, "0.0.0.0", () => console.log(`API server listening on port ${port}`)); // tslint:disable-line
|
2021-09-05 13:53:46 +03:00
|
|
|
|
|
|
|
startBackgroundTasks();
|