-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (52 loc) · 1.39 KB
/
index.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
const fs = require('fs/promises');
require('dotenv').config();
const express = require('express');
const app = express();
const port = 3000;
let { whitelistedUsers } = require('./whitelist.json');
app.get('/whitelist/:userId', (req, res) => {
res.json({ whitelisted: whitelistedUsers.includes(req.params.userId) });
});
app.post('/whitelist/:userId', async (req, res) => {
if (!req.headers.authorization) {
res.sendStatus(400);
return;
}
if (req.headers.authorization === `WlKey ${process.env.WL_KEY}`) {
whitelistedUsers.push(req.params.userId);
await saveUsers();
res.sendStatus(200);
} else {
res.sendStatus(401);
}
});
app.post('/unwhitelist/:userId', async (req, res) => {
if (!req.headers.authorization) {
res.sendStatus(400);
return;
}
if (req.headers.authorization === `WlKey ${process.env.WL_KEY}`) {
whitelistedUsers = whitelistedUsers.filter(
(userId) => userId !== req.params.userId
);
await saveUsers();
res.sendStatus(200);
} else {
res.sendStatus(401);
}
});
const saveUsers = async () => {
try {
await fs.writeFile(
'whitelist.json',
JSON.stringify({ whitelistedUsers: whitelistedUsers }, null, 2)
);
} catch (err) {
console.error('Error saving users:', err);
}
};
process.on('SIGTERM', saveUsers);
process.on('SIGINT', saveUsers);
app.listen(port, () => {
console.log('WL listening');
});