2021-05-15 01:56:08 +08:00
|
|
|
const express = require("express");
|
2021-06-07 00:32:37 +08:00
|
|
|
const { config } = require("dotenv");
|
2021-06-16 07:46:37 +08:00
|
|
|
const path = require("path");
|
2021-07-08 19:30:06 +08:00
|
|
|
const MonkeyError = require("./handlers/error");
|
2021-06-07 00:32:37 +08:00
|
|
|
config({ path: path.join(__dirname, ".env") });
|
2021-09-10 06:39:10 +08:00
|
|
|
const CronJob = require("cron").CronJob;
|
2021-06-01 11:47:58 +08:00
|
|
|
const cors = require("cors");
|
|
|
|
const admin = require("firebase-admin");
|
2021-05-15 01:56:08 +08:00
|
|
|
|
2021-06-07 00:32:37 +08:00
|
|
|
const serviceAccount = require("./credentials/serviceAccountKey.json");
|
2021-08-18 07:33:14 +08:00
|
|
|
const { connectDB, mongoDB } = require("./init/mongodb");
|
2021-05-20 06:39:18 +08:00
|
|
|
|
2021-06-16 07:47:34 +08:00
|
|
|
const PORT = process.env.PORT || 5005;
|
|
|
|
|
2021-06-01 11:47:58 +08:00
|
|
|
// MIDDLEWARE & SETUP
|
2021-05-15 01:56:08 +08:00
|
|
|
const app = express();
|
2021-06-07 00:32:37 +08:00
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
app.use(express.json());
|
2021-08-17 01:01:52 +08:00
|
|
|
app.use(cors());
|
2021-05-15 01:56:08 +08:00
|
|
|
|
2021-08-28 04:52:49 +08:00
|
|
|
app.set("trust proxy", 1);
|
|
|
|
|
2021-09-06 07:52:05 +08:00
|
|
|
app.use((req, res, next) => {
|
|
|
|
if (process.env.MAINTENANCE === "true") {
|
2021-09-06 07:52:32 +08:00
|
|
|
res.status(503).json({ message: "Server is down for maintenance" });
|
2021-09-06 07:52:05 +08:00
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-16 07:46:37 +08:00
|
|
|
const userRouter = require("./api/routes/user");
|
|
|
|
app.use("/user", userRouter);
|
2021-07-06 21:52:36 +08:00
|
|
|
const configRouter = require("./api/routes/config");
|
|
|
|
app.use("/config", configRouter);
|
2021-07-06 22:22:05 +08:00
|
|
|
const resultRouter = require("./api/routes/result");
|
2021-07-09 05:22:18 +08:00
|
|
|
app.use("/results", resultRouter);
|
2021-07-09 05:34:36 +08:00
|
|
|
const presetRouter = require("./api/routes/preset");
|
|
|
|
app.use("/presets", presetRouter);
|
2021-08-28 03:10:00 +08:00
|
|
|
const quoteRatings = require("./api/routes/quote-ratings");
|
|
|
|
app.use("/quote-ratings", quoteRatings);
|
2021-08-29 21:30:45 +08:00
|
|
|
const psaRouter = require("./api/routes/psa");
|
|
|
|
app.use("/psa", psaRouter);
|
2021-09-06 06:57:07 +08:00
|
|
|
const leaderboardsRouter = require("./api/routes/leaderboards");
|
2021-09-07 00:35:03 +08:00
|
|
|
app.use("/leaderboard", leaderboardsRouter);
|
2021-05-15 01:56:08 +08:00
|
|
|
|
2021-06-07 01:26:12 +08:00
|
|
|
app.use(function (e, req, res, next) {
|
2021-07-13 22:11:38 +08:00
|
|
|
let uid = undefined;
|
|
|
|
if (req.decodedToken) {
|
|
|
|
uid = req.decodedToken.uid;
|
|
|
|
}
|
2021-08-18 07:41:33 +08:00
|
|
|
let monkeyError;
|
|
|
|
if (e.errorID) {
|
|
|
|
//its a monkey error
|
|
|
|
monkeyError = e;
|
|
|
|
} else {
|
|
|
|
//its a server error
|
|
|
|
monkeyError = new MonkeyError(e.status, e.message, e.stack, uid);
|
|
|
|
}
|
2021-08-18 03:00:29 +08:00
|
|
|
if (process.env.MODE !== "dev" && monkeyError.status > 400) {
|
2021-09-10 07:03:01 +08:00
|
|
|
Logger.log(
|
|
|
|
`system_error`,
|
|
|
|
`${monkeyError.status} ${monkeyError.message}`,
|
|
|
|
monkeyError.uid
|
|
|
|
);
|
2021-08-18 07:33:14 +08:00
|
|
|
mongoDB().collection("errors").insertOne({
|
2021-08-18 03:00:29 +08:00
|
|
|
_id: monkeyError.errorID,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
status: monkeyError.status,
|
|
|
|
uid: monkeyError.uid,
|
|
|
|
message: monkeyError.message,
|
|
|
|
stack: monkeyError.stack,
|
|
|
|
});
|
|
|
|
}
|
2021-07-08 19:30:06 +08:00
|
|
|
return res.status(e.status || 500).json(monkeyError);
|
2021-05-15 01:56:08 +08:00
|
|
|
});
|
2021-06-14 14:49:23 +08:00
|
|
|
|
2021-08-11 07:02:17 +08:00
|
|
|
app.get("/test", (req, res) => {
|
|
|
|
res.send("Hello World!");
|
|
|
|
});
|
|
|
|
|
2021-09-10 06:39:10 +08:00
|
|
|
const LeaderboardsDAO = require("./dao/leaderboards");
|
|
|
|
|
2021-06-16 07:47:34 +08:00
|
|
|
app.listen(PORT, async () => {
|
|
|
|
console.log(`listening on port ${PORT}`);
|
2021-06-14 14:49:23 +08:00
|
|
|
await connectDB();
|
|
|
|
admin.initializeApp({
|
|
|
|
credential: admin.credential.cert(serviceAccount),
|
|
|
|
});
|
|
|
|
console.log("Database Connected");
|
2021-09-06 06:57:07 +08:00
|
|
|
|
2021-09-10 08:04:40 +08:00
|
|
|
let lbjob = new CronJob("30 4/5 * * * *", async () => {
|
2021-09-10 07:50:47 +08:00
|
|
|
LeaderboardsDAO.update("time", "15", "english");
|
|
|
|
LeaderboardsDAO.update("time", "60", "english");
|
2021-09-10 06:39:10 +08:00
|
|
|
});
|
|
|
|
lbjob.start();
|
|
|
|
});
|