From adb19e2e344bcecfd5843514a993c54f0645200c Mon Sep 17 00:00:00 2001 From: Akif9748 Date: Thu, 25 May 2023 16:15:50 +0300 Subject: [PATCH] setup and index is now differnt --- README.md | 4 +--- routes/.js | 32 ++------------------------------ routes/setup.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 33 deletions(-) create mode 100644 routes/setup.js diff --git a/README.md b/README.md index c59bc91..0796139 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/routes/.js b/routes/.js index 3e5d5a1..686d8e9 100644 --- a/routes/.js +++ b/routes/.js @@ -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; \ No newline at end of file diff --git a/routes/setup.js b/routes/setup.js new file mode 100644 index 0000000..368e1f0 --- /dev/null +++ b/routes/setup.js @@ -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; \ No newline at end of file