-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
32 lines (28 loc) · 925 Bytes
/
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
const intraConf = require('./config.json');
const express = require('express')
const discordClient = require('../discord/client.js')
const fs = require('fs');
const app = express()
let intraAuth = function (req, res, next) {
endpointName = req.url.split('/').pop()
if (!(req.headers['x-secret'] === intraConf.hooks_secrets[endpointName])) {
console.log('unauthorized');
res.sendStatus(401);
return;
}
next();
}
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(intraAuth);
fs.readdir('./intra/endpoints/', (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
const endpoint = require(`./endpoints/${file}`);
let endpointURL = `/${file.split('.')[0]}`;
app.post(endpointURL, endpoint.bind(null, discordClient, intraConf));
});
});
app.listen(intraConf.port, () => {
console.log(`Listening intrahooks at http://localhost:${intraConf.port}`);
});