monkeytype/backend/server.js

97 lines
2.7 KiB
JavaScript
Raw Normal View History

const express = require("express");
2021-06-07 00:32:37 +08:00
const { config } = require("dotenv");
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") });
const CronJob = require("cron").CronJob;
const cors = require("cors");
const admin = require("firebase-admin");
2021-06-07 00:32:37 +08:00
const serviceAccount = require("./credentials/serviceAccountKey.json");
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;
// MIDDLEWARE & SETUP
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-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();
}
});
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);
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-06-07 01:26:12 +08:00
app.use(function (e, req, res, next) {
let uid = undefined;
if (req.decodedToken) {
uid = req.decodedToken.uid;
}
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);
}
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
);
mongoDB().collection("errors").insertOne({
_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-06-14 14:49:23 +08:00
2021-08-11 07:02:17 +08:00
app.get("/test", (req, res) => {
res.send("Hello World!");
});
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");
});
lbjob.start();
});