2022-01-04 02:18:28 +05:00
|
|
|
const config = require("../config"),
|
|
|
|
utils = require("./utils"),
|
|
|
|
CheckAuth = require("./auth/CheckAuth");
|
|
|
|
|
2022-07-26 17:20:10 +05:00
|
|
|
module.exports.init = async(client) => {
|
2022-01-04 02:18:28 +05:00
|
|
|
/* 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"),
|
2022-01-12 21:39:34 +05:00
|
|
|
profileRouter = require("./routes/profile"),
|
2022-01-13 18:55:13 +05:00
|
|
|
userRouter = require("./routes/user"),
|
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;
|
2022-04-03 12:47:54 +05:00
|
|
|
req.locale = req.user ? (req.user.locale === "ru" ? "ru-RU" : "uk-UA") : "ru-RU";
|
2022-01-04 02:18:28 +05:00
|
|
|
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);
|
2022-01-12 20:31:47 +05:00
|
|
|
req.convertTime = (time) => req.client.convertTime(time, "to", true, req.locale);
|
2022-01-13 00:26:23 +05:00
|
|
|
}
|
2022-01-04 02:18:28 +05:00
|
|
|
next();
|
|
|
|
})
|
|
|
|
.use("/api", discordAPIRouter)
|
|
|
|
.use("/logout", logoutRouter)
|
|
|
|
.use("/manage", guildManagerRouter)
|
|
|
|
.use("/stats", guildStatsRouter)
|
2022-01-12 21:39:34 +05:00
|
|
|
.use("/profile", profileRouter)
|
2022-01-13 18:55:13 +05:00
|
|
|
.use("/user", userRouter)
|
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("/");
|
2022-02-13 16:50:44 +05:00
|
|
|
|
2022-01-04 02:18:28 +05:00
|
|
|
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")}`);
|
|
|
|
});
|
|
|
|
};
|