akf-forum/routes/.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-04-06 21:14:46 +03:00
const { UserModel, ThreadModel, MessageModel } = require("../models")
2022-03-21 23:53:22 +03:00
const { Router } = require("express");
const app = Router();
const fs = require("fs");
2022-03-21 23:53:22 +03:00
2022-04-06 21:14:46 +03:00
app.get("/", async (req, res) => {
2022-08-26 16:27:29 +03:00
2022-04-03 21:01:55 +03:00
const
mem = process.memoryUsage().heapUsed / Math.pow(2, 20),
users = await UserModel.count({ deleted: false }),
threads = await ThreadModel.count({ state: "OPEN" }),
messages = await MessageModel.count({ deleted: false });
2022-03-21 23:53:22 +03:00
2022-08-27 10:31:16 +03:00
res.reply("index", { mem, users, threads, messages })
2022-03-21 23:53:22 +03:00
})
app.get("/setup", async (req, res) => {
if (await UserModel.exists({ admin: true })) return res.error(400, "You have already setuped the site.");
res.reply("setup");
})
app.post("/setup", async (req, res) => {
if (await UserModel.exists({ admin: true })) return res.error(400, "You have already setuped the site.");
let original = {};
try {
original = JSON.parse(fs.readFileSync("./config.json", "utf8"));
} catch (e) {
try {
original = JSON.parse(fs.readFileSync("./config.json.example", "utf8"));
} catch (e) { }
}
const content = req.body;
for (const key in content)
if (key in original && content[key])
original[key] = content[key];
fs.writeFileSync("./config.json", JSON.stringify(original,null,4));
require.cache[require.resolve("../config.json")] = require("../config.json");
res.redirect("/register");
})
2022-03-21 23:53:22 +03:00
module.exports = app;