-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
90 lines (79 loc) · 2.3 KB
/
server.js
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
79
80
81
82
83
84
85
86
87
88
89
90
import express from 'express';
import cors from 'cors';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const port = 3001;
const app = express();
app.use(cors());
app.use(express.json());
const apiEndpoint = '/api';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const bansFilePath = path.join(__dirname, 'static', 'bans.json');
function readBans() {
const data = fs.readFileSync(bansFilePath, 'utf8');
return JSON.parse(data);
}
function writeBans(bans) {
fs.writeFileSync(bansFilePath, JSON.stringify(bans, null, 2), 'utf8');
}
app.get(apiEndpoint + '/getBans', (req, res) => {
try {
const bans = readBans();
res.json(bans);
} catch (error) {
console.error(error)
res.status(500).json({ error: 'Failed to read bans' });
}
});
app.post(apiEndpoint + '/addBan', (req, res) => {
try {
const newBan = { ...req.body, approved: 0 };
const bans = readBans();
bans.push(newBan);
writeBans(bans);
res.json({ message: 'Ban added successfully', ban: newBan });
} catch (error) {
console.error(error)
res.status(500).json({ error: 'Failed to add ban' });
}
});
app.post(apiEndpoint + '/approveBan', (req, res) => {
try {
const { name, description } = req.body;
const bans = readBans();
const banIndex = bans.findIndex((ban) => ban.name === name && ban.description === description);
if (banIndex !== -1) {
bans[banIndex].approved += 1;
writeBans(bans);
res.json({ message: 'Ban approved successfully', ban: bans[banIndex] });
} else {
res.status(404).json({ error: 'Ban not found' });
}
} catch (error) {
console.error(error)
res.status(500).json({ error: 'Failed to approve ban' });
}
});
app.post(apiEndpoint + '/rejectBan', (req, res) => {
try {
const { name, description } = req.body;
const bans = readBans();
const updatedBans = bans.filter(
(ban) => !(ban.name === name && ban.description === description)
);
if (bans.length !== updatedBans.length) {
writeBans(updatedBans);
res.json({ message: 'Ban rejected successfully' });
} else {
res.status(404).json({ error: 'Ban not found' });
}
} catch (error) {
console.error(error)
res.status(500).json({ error: 'Failed to reject ban' });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});