-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (70 loc) · 2.74 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
const fs = require('fs');
const path = require('path');
const http = require('http')
const https = require('https')
const Tracker = require('./lib/tracker/tracker');
const taskScheduler = require('./lib/taskScheduler/taskScheduler');
const websocketLib = require('./lib/websocket/websocket')
const betterLogging = require('better-logging');
const { MessageConstructionStrategy } = betterLogging;
betterLogging(console, {
messageConstructionStrategy: MessageConstructionStrategy.NONE,
});
// Uncomment below for debug logging
// betterLogging(console, {
// logLevels: {
// debug: 0,
// error: 0,
// info: 0,
// line: 0,
// log: 0,
// warn: 0,
// }
// });
let startTime = Date.now();
load();
async function load() {
console.debug("Debug logging enabled!")
console.info(`Starting donation tracker (running on Node.JS ${process.version})`)
// Set the program's base directory.
global.__rootdir = __dirname;
// Delete cached database entries.
await deleteCachedFiles();
// Create the tracker and database objects.
console.debug("Creating tracker object...")
new Tracker(start);
}
// Wait for tracker object to initialize.
async function start(tracker, database) {
console.debug("Tracker loaded successfully!")
// Check for valid currency.
console.debug('Checking currensies...')
if (tracker.currencies[tracker.config.paypal.currency] === undefined) {
console.error('Currency is invalid. Please make sure it\'s ISO 4217 compliant and compatible with Paypal.')
console.error('A list of compatible currencies can be found at https://developer.paypal.com/docs/reports/reference/paypal-supported-currencies/')
process.exit();
}
// Set up SSL (if enabled)
console.debug('Setting up SSL...')
if (tracker.config.ssl.enabled) https.createServer({
key: fs.readFileSync(tracker.config.ssl.keyPath, 'utf8'),
cert: fs.readFileSync(tracker.config.ssl.certPath, 'utf8')
}, tracker.server).listen(tracker.config.port, () => console.info(`Donation tracker is running at ${tracker.url}`));
else http.createServer(tracker.server).listen(tracker.config.port, () => console.info(`Donation tracker is running at ${tracker.url}`));
// Setting up task scheduler...
taskScheduler.start();
console.debug('Tracker start up complete!');
console.debug(`Startup time: ${Date.now() - startTime}ms`)
}
async function deleteCachedFiles() {
return new Promise(async (resolve, reject) => {
fs.readdir('./cache', (err, files) => {
if (err) console.log(err)
files.forEach((file, index) => {
const fileDir = path.join(__dirname, 'cache', file);
if (file !== '.empty-directory' && file !== 'sessions') fs.unlinkSync(fileDir);
if (files.length - 1 >= index) return resolve();
});
});
})
}