aboutsummaryrefslogtreecommitdiff
path: root/motto/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'motto/index.js')
-rw-r--r--motto/index.js122
1 files changed, 48 insertions, 74 deletions
diff --git a/motto/index.js b/motto/index.js
index ec14a33..26d70bd 100644
--- a/motto/index.js
+++ b/motto/index.js
@@ -1,8 +1,9 @@
const express = require("express");
-const sqlite3 = require("sqlite3");
const rateLimit = require("express-rate-limit");
-const app = express();
-// const fs = require("fs");
+const db = require("../db");
+const app = express.Router();
+
+const fs = require("fs");
const apiLimiter = rateLimit({
windowMs: 10 * 60 * 1000,
@@ -10,95 +11,68 @@ const apiLimiter = rateLimit({
message: "Access overflow!",
});
-app.use(express.urlencoded({ extended: true }));
-app.use(express.json());
-
-let db;
-function create_db() {
- if (db) db.close();
-
- db = new sqlite3.Database("db.db", sqlite3.OPEN_READWRITE, (err) => {
- if (err) console.error(err.message);
- console.log("Connected to the database");
- });
-
- db.run(
- "CREATE TABLE IF NOT EXISTS theme(id INTEGER PRIMARY KEY AUTOINCREMENT, main TEXT NOT NULL, description TEXT NOT NULL, votes INTEGER DEFAULT 0, hidden BOOL DEFAULT 0 UNIQUE(main, description))",
- (err) => {
- if (err) console.error(err.message);
- }
- );
-}
-
-function insert(main, description, votes) {
- db.run(`INSERT INTO theme(main, description, votes) VALUES(?, ?, ?)`, [main, description, votes], (err) => {
+app.get("/sync", (req, res) => {
+ fs.readFile(__dirname + "/list.txt", "utf8", (err, data) => {
if (err) {
- console.error(err.message);
- return;
+ console.error(err);
+ return res.send("error");
}
+ const lines = data.split("\n");
+ lines.forEach(async (line) => {
+ const split = line.split(" - ");
+ try {
+ if (split.length >= 2)
+ await db.query("INSERT INTO theme (main, description) VALUES (?, ?)", split.slice(0, 2));
+ else console.log(line);
+ } catch (e) {
+ console.error(e);
+ } finally {
+ res.send("ok");
+ }
+ });
});
-}
-
-// app.get("/sync", (req, res) => {
-// fs.unlinkSync("db.db");
-// fs.closeSync(fs.openSync("db.db", "w"));
-// create_db();
-// fs.readFile("list.txt", "utf8", (err, data) => {
-// if (err) return;
-// const lines = data.split("\n");
-// lines.forEach((line) => {
-// const split = line.split(" - ");
-// if (split[0] && split[1]) insert(split[0], split[1], 0);
-// else console.log(line);
-// });
-// });
-// res.send("ok");
-// });
+});
app.use("/", express.static(__dirname + "/public"));
app.use("/api/", apiLimiter);
-app.get("/api/list", (req, res) => {
- db.all("SELECT * FROM theme WHERE hidden = 0 ORDER BY votes DESC", (err, all) => {
- if (err) {
- res.send("error");
- console.error(err.message);
- return;
- }
- res.send(all);
- });
+app.get("/api/list", async (req, res) => {
+ try {
+ const themes = await db.query("SELECT * FROM theme" /* WHERE hidden = FALSE ORDER BY votes DESC"*/);
+ res.json(themes);
+ } catch (e) {
+ console.error(e);
+ res.send("error");
+ }
});
-app.post("/api/add", (req, res) => {
+app.post("/api/add", async (req, res) => {
console.log(req.body.main, req.body.description);
- if (!req.body.main || !req.body.description) res.send("error");
- insert(req.body.main, req.body.description, 1);
- res.send("ok");
+ if (!req.body.main || !req.body.description) return res.send("error");
+ try {
+ await db.query("INSERT INTO theme (main, description) VALUES (?, ?)", [req.body.main, req.body.description]);
+ res.send("ok");
+ } catch (e) {
+ console.error(e);
+ res.send("error");
+ }
});
-app.post("/api/vote", (req, res) => {
+app.post("/api/vote", async (req, res) => {
console.log(req.body.id, req.body.vote);
if (req.body.vote < -1 || req.body.vote > 1) {
res.send("error");
return;
}
- db.all("UPDATE theme SET votes = votes + ? WHERE id = ?", [req.body.vote, req.body.id], (err) => {
- if (err) {
- res.send("error");
- console.error(err.message);
- return;
- }
- });
- res.send("ok");
-});
-
-app.on("close", () => {
- console.log("CLOSE");
- db.close();
+ try {
+ await db.query("UPDATE theme SET votes = votes + ? WHERE id = ?", [req.body.vote, req.body.id]);
+ res.send("ok");
+ } catch (e) {
+ console.error(e);
+ res.send("error");
+ }
});
-create_db();
-console.log("Listening on port 5005");
-app.listen(5005);
+module.exports = app;