forked from Lawin0129/LawinServerV2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (74 loc) · 2.85 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
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
91
92
93
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const fs = require("fs");
const rateLimit = require("express-rate-limit");
const jwt = require("jsonwebtoken");
const config = JSON.parse(fs.readFileSync("./Config/config.json").toString());
const cron = require('node-cron');
const log = require("./structs/log.js");
const error = require("./structs/error.js");
const functions = require("./structs/functions.js");
if (!fs.existsSync("./ClientSettings")) fs.mkdirSync("./ClientSettings");
global.JWT_SECRET = functions.MakeID();
const PORT = 8080;
const tokens = JSON.parse(fs.readFileSync("./tokenManager/tokens.json").toString());
for (let tokenType in tokens) {
for (let tokenIndex in tokens[tokenType]) {
let decodedToken = jwt.decode(tokens[tokenType][tokenIndex].token.replace("eg1~", ""));
if (DateAddHours(new Date(decodedToken.creation_date), decodedToken.hours_expire).getTime() <= new Date().getTime()) {
tokens[tokenType].splice(Number(tokenIndex), 1);
}
}
}
fs.writeFileSync("./tokenManager/tokens.json", JSON.stringify(tokens, null, 2));
global.accessTokens = tokens.accessTokens;
global.refreshTokens = tokens.refreshTokens;
global.clientTokens = tokens.clientTokens;
global.exchangeCodes = [];
mongoose.connect(config.mongodb.database, () => {
log.backend("App successfully connected to MongoDB!");
});
mongoose.connection.on("error", err => {
log.error("MongoDB failed to connect, please make sure you have MongoDB installed and running.");
throw err;
});
if (config.shopAPI.useAPI) {
cron.schedule('2 0 * * *', () => {
functions.makeShop();
console.log('Generating Item Shop');
}, {
scheduled: true,
timezone: 'Europe/Berlin',
});
}
app.use(rateLimit({ windowMs: 0.5 * 60 * 1000, max: 45 }));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
fs.readdirSync("./routes").forEach(fileName => {
app.use(require(`./routes/${fileName}`));
});
app.listen(PORT, () => {
log.backend(`App started listening on port ${PORT}`);
require("./xmpp/xmpp.js");
require("./DiscordBot");
}).on("error", async (err) => {
if (err.code == "EADDRINUSE") {
log.error(`Port ${PORT} is already in use!\nClosing in 3 seconds...`);
await functions.sleep(3000)
process.exit(0);
} else throw err;
});
// if endpoint not found, return this error
app.use((req, res, next) => {
error.createError(
"errors.com.epicgames.common.not_found",
"Sorry the resource you were trying to find could not be found",
undefined, 1004, undefined, 404, res
);
});
function DateAddHours(pdate, number) {
let date = pdate;
date.setHours(date.getHours() + number);
return date;
}