setup and index is now differnt

This commit is contained in:
Akif9748 2023-05-25 16:15:50 +03:00
parent 667c0222e9
commit adb19e2e34
3 changed files with 37 additions and 33 deletions

View File

@ -59,10 +59,8 @@ Akf-forum has got an API for AJAX (fetch), other clients etc. And, you can learn
- change category name
- _id
- add support for transition around gravatar
- forum setup page rewrite and directly open a router
- login w/ email
- BETTER SETUP PAGE
- add used open source libraries to README.md
### front-end
- text alling center body
- add a css file for CodeMirror in threads / send message ok

View File

@ -1,7 +1,6 @@
const { UserModel, ThreadModel, MessageModel } = require("../models")
const { Router } = require("express");
const app = Router();
const fs = require("fs");
app.get("/", async (req, res) => {
@ -11,35 +10,8 @@ app.get("/", async (req, res) => {
threads = await ThreadModel.count({ state: "OPEN" }),
messages = await MessageModel.count({ deleted: false });
res.reply("index", { mem, users, threads, messages })
res.reply("index", { mem, users, threads, messages });
})
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");
})
});
module.exports = app;

34
routes/setup.js Normal file
View File

@ -0,0 +1,34 @@
const { UserModel } = require("../models")
const { Router } = require("express");
const app = Router();
const fs = require("fs");
app.use(async (req, res, next) => {
if (await UserModel.exists({ admin: true })) return res.error(400, "You have already setuped the site.");
next();
});
app.get("/", async (req, res) => res.reply("setup"))
app.post("/", async (req, res) => {
let original = {};
try {
original = JSON.parse(fs.readFileSync("./config.json", "utf8"));
} catch (e) {
try {
original = JSON.parse(fs.readFileSync("./config.json.example", "utf8"));
// eslint-disable-next-line no-empty
} 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");
})
module.exports = app;