JaBa/dashboard/app.js

78 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-01-04 02:18:28 +05:00
const config = require("../config"),
utils = require("./utils"),
CheckAuth = require("./auth/CheckAuth");
module.exports.load = async(client) => {
/* Init express app */
const express = require("express"),
session = require("express-session"),
path = require("path"),
app = express();
/* Routers */
const mainRouter = require("./routes/index"),
discordAPIRouter = require("./routes/discord"),
logoutRouter = require("./routes/logout"),
profileRouter = require("./routes/profile"),
2022-01-04 02:18:28 +05:00
guildStatsRouter = require("./routes/guild-stats"),
guildManagerRouter = require("./routes/guild-manager"),
docsManagerRouter = require("./routes/docs");
/* App configuration */
app
// For post methods
.use(express.json())
.use(express.urlencoded({ extended: true }))
// Set the engine to html (for ejs template)
.engine("html", require("ejs").renderFile)
.set("view engine", "ejs")
// Set the css and js folder to ./public
.use(express.static(path.join(__dirname, "/public")))
// Set the ejs templates to ./views
.set("views", path.join(__dirname, "/views"))
// Set the dashboard port
.set("port", config.dashboard.port)
// Set the express session password and configuration
.use(session({ secret: config.dashboard.expressSessionPassword, resave: false, saveUninitialized: false }))
// Multi languages support
.use(async function(req, res, next) {
req.user = req.session.user;
req.client = client;
req.locale = req.user ? (req.user.locale === "ru" ? "ru-RU" : "en-US") : "en-US";
if (req.user && req.url !== "/") req.userInfos = await utils.fetchUser(req.user, req.client);
if (req.user) {
req.translate = req.client.translations.get(req.locale);
req.printDate = (date) => req.client.printDate(date, null, req.locale);
req.convertTime = (time) => req.client.convertTime(time, "to", true, req.locale);
2022-01-04 02:18:28 +05:00
};
next();
})
.use("/api", discordAPIRouter)
.use("/logout", logoutRouter)
.use("/manage", guildManagerRouter)
.use("/stats", guildStatsRouter)
.use("/profile", profileRouter)
2022-01-04 02:18:28 +05:00
.use("/", mainRouter)
.use("/docs", docsManagerRouter)
.use(CheckAuth, function(req, res) {
res.status(404).render("404", {
user: req.userInfos,
translate: req.translate,
currentURL: `${req.protocol}://${req.get("host")}${req.originalUrl}`
});
})
.use(CheckAuth, function(err, req, res) {
console.error(err.stack);
if (!req.user) return res.redirect("/");
res.status(500).render("500", {
user: req.userInfos,
translate: req.translate,
currentURL: `${req.protocol}://${req.get("host")}${req.originalUrl}`
});
});
// Listen
app.listen(app.get("port"), () => {
console.log(`JaBa Dashboard is listening on port ${app.get("port")}`);
});
};