-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
69 lines (62 loc) · 1.83 KB
/
api.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
const express = require('express'),
ipfilter = require('express-ipfilter').IpFilter,
IpDeniedError = require('express-ipfilter').IpDeniedError,
RE2 = require('re2');
require('dotenv').config();
const env = process.env;
global.pool = require('./utils/pool.js').create('4tunes');
global.regex = {
uuid: new RE2(/^([0-9a-f]{8})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{12})$/i),
id: new RE2(/^([0-9]{17,19})$/),
hexColor: new RE2(/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i)
};
global.badRequest = require('./routes/badRequest.js');
const app = express();
app.use(express.json());
app.set('view engine', 'ejs');
app.disable('x-powered-by');
app.disable('etag');
app.listen(env.API_PORT);
// app.use((req, res, next) => {
// console.log(req.headers['x-forwarded-for'], req.connection.remoteAddress, req.ip);
// next();
// });
app.use(ipfilter(['::ffff:127.0.0.1', '::1'], { mode: 'allow', log: true, logLevel: 'deny' }));
app.use((err, req, res, _next) => {
if (err instanceof IpDeniedError) {
res.status(401)
} else {
res.status(err.status || 500)
}
const data = {
"error": {
"code": 401,
"message": "This IP address has been added to the blacklist."
}
};
res.json(data);
});
app.use('/minecraft/players', require('./routes/minecraft.js'));
app.use('/users', require('./routes/users.js'));
app.use('/guilds', require('./routes/guilds.js'));
app.use((req, res) => {
res.status(404);
const data = {
"error": {
"code": 404,
"message": "Not Found"
}
};
res.json(data);
});
app.use((err, req, res, next) => {
console.error(err);
res.status(500);
const data = {
"error": {
"code": 500,
"message": "Internal Server Error"
}
};
res.json(data);
});