2022-09-09 15:34:12 +03:00
|
|
|
const { def_theme, forum_name, desp } = require("./config.json"),
|
|
|
|
{ UserModel, BanModel } = require("./models"),
|
2022-09-04 22:19:19 +03:00
|
|
|
rateLimit = require('express-rate-limit'),
|
2022-08-29 19:31:59 +03:00
|
|
|
ipBlock = require('express-ip-block'),
|
|
|
|
session = require('express-session'),
|
|
|
|
bodyParser = require('body-parser'),
|
|
|
|
port = process.env.PORT || 3000,
|
|
|
|
mongoose = require("mongoose"),
|
|
|
|
express = require('express'),
|
|
|
|
fs = require("fs"),
|
|
|
|
app = express();
|
2022-08-31 14:44:28 +03:00
|
|
|
|
2022-08-29 19:31:59 +03:00
|
|
|
app.ips = [];
|
2022-09-09 16:29:36 +03:00
|
|
|
|
2022-08-09 19:16:34 +03:00
|
|
|
require("dotenv").config();
|
2022-08-29 19:31:59 +03:00
|
|
|
mongoose.connect(process.env.MONGO_DB_URL,
|
2022-08-31 14:44:28 +03:00
|
|
|
async () => console.log("Database is connected with", (app.ips = await BanModel.find({})).length, "banned IPs"));
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-03-13 16:16:46 +03:00
|
|
|
app.set("view engine", "ejs");
|
2022-08-29 19:31:59 +03:00
|
|
|
|
2022-09-01 14:02:47 +03:00
|
|
|
app.use(
|
|
|
|
session({ secret: 'secret', resave: true, saveUninitialized: true }),
|
2022-08-29 19:31:59 +03:00
|
|
|
express.static("public"), express.json(), ipBlock(app.ips),
|
|
|
|
async (req, res, next) => {
|
2022-09-04 22:19:19 +03:00
|
|
|
req.headers["x-forwarded-for"];
|
2022-08-31 14:44:28 +03:00
|
|
|
req.user = await UserModel.get(req.session.userID);
|
2022-08-27 10:31:16 +03:00
|
|
|
res.reply = (page, options = {}, status = 200) => res.status(status)
|
2022-09-09 15:34:12 +03:00
|
|
|
.render(page, { user: req.user, theme: req.user?.theme || def_theme, forum_name, desp, ...options });
|
2022-08-27 10:31:16 +03:00
|
|
|
|
|
|
|
res.error = (type, error) => res.reply("error", { type, error }, type);
|
|
|
|
|
2022-08-24 22:10:23 +03:00
|
|
|
if (req.user?.deleted) {
|
2022-08-29 19:31:59 +03:00
|
|
|
req.session.destroy();
|
|
|
|
return res.error(403, "Your account has been deleted.");
|
2022-08-24 22:09:21 +03:00
|
|
|
}
|
2022-08-11 03:12:40 +03:00
|
|
|
next();
|
2022-09-01 14:02:47 +03:00
|
|
|
}, rateLimit({
|
2022-09-09 15:34:12 +03:00
|
|
|
windowMs: 60_000, max: 20,
|
2022-09-05 23:44:38 +03:00
|
|
|
handler: (req, res, next, opts) => !req.user?.admin ? res.error(opts.statusCode, "You are begin ratelimited") : next()
|
2022-09-01 14:02:47 +03:00
|
|
|
}), bodyParser.urlencoded({ extended: true })
|
2022-08-29 19:31:59 +03:00
|
|
|
);
|
|
|
|
|
2022-03-22 00:10:29 +03:00
|
|
|
for (const file of fs.readdirSync("./routes"))
|
2022-08-29 19:31:59 +03:00
|
|
|
app.use("/" + file.replace(".js", ""), require(`./routes/${file}`));
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-08-11 03:12:40 +03:00
|
|
|
app.all("*", (req, res) => res.error(404, "We have not got this page."));
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-09-09 15:34:12 +03:00
|
|
|
app.listen(port, () => console.log(forum_name + "-forum on port:", port));
|