1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
const express = require("express");
const rateLimit = require("express-rate-limit");
const db = require("../db");
const app = express.Router();
const fs = require("fs");
const apiLimiter = rateLimit({
windowMs: 10 * 60 * 1000,
max: 100,
message: "Access overflow!",
});
app.get("/sync", (req, res) => {
fs.readFile(__dirname + "/list.txt", "utf8", (err, data) => {
if (err) {
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.use("/", express.static(__dirname + "/public"));
app.use("/api/", apiLimiter);
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", async (req, res) => {
console.log(req.body.main, req.body.description);
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", async (req, res) => {
console.log(req.body.id, req.body.vote);
if (req.body.vote < -1 || req.body.vote > 1) {
res.send("error");
return;
}
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");
}
});
module.exports = app;
|