forked from musiqpad/mqp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
86 lines (73 loc) · 2.53 KB
/
start.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
// eslint-disable-next-line
'use strict';
// NCONF
const nconf = require('nconf');
const fs = require('fs-extra');
const hjson = require('hjson');
const crypto = require('crypto');
const hjsonWrapper = {
parse: (text) => hjson.parse(text, { keepWsc: true, }),
stringify: (text) => hjson.stringify(text, { keepWsc: true, quotes: 'always', bracesSameLine: true }),
};
if (!fileExistsSync('config.hjson')) {
fs.copySync('config.example.hjson', 'config.hjson');
}
nconf.argv().env().file({ file: 'config.hjson', format: hjsonWrapper });
if (!nconf.get('tokenSecret')) {
const random = crypto.randomBytes(256);
nconf.set('tokenSecret', random.toString('hex'));
nconf.save();
}
// Modules
const SocketServer = require('./socketserver/socketserver');
const path = require('path');
const webserver = require('./webserver/app');
const log = new(require('basic-logger'))({
showTimestamp: true,
prefix: 'SocketServer',
});
let server;
const webConfig = `// THIS IS AN AUTOMATICALLY GENERATED FILE\n\nvar config=JSON.parse('${JSON.stringify({
useSSL: nconf.get('useSSL'),
serverPort: nconf.get('socketServer:port'),
selfHosted: !0,
serverHost: nconf.get('socketServer:host')
})}')`;
if (nconf.get('hostWebserver')) {
fs.writeFileSync(path.join(__dirname, '/webserver/public/lib/js', 'webconfig.js'), webConfig);
server = (nconf.get('socketServer:port') === nconf.get('webServer:port') || nconf.get('socketServer:port') === '') ? webserver.server : null;
}
if (nconf.get('apis:musiqpad:sendLobbyStats') && (!nconf.get('apis:musiqpad:key') || nconf.get('apis:musiqpad:key') === '')) {
log.error('In order to send stats to the lobby you must generate an key here: https://musiqpad.com/lounge');
process.exit();
}
fs.writeFileSync(path.join(__dirname, '', 'webconfig.js'), webConfig);
const socketServer = new SocketServer(server);
process.on('uncaughtException', (err) => {
console.log(err);
console.log(err.stack);
socketServer.gracefulExit();
});
process.on('exit', socketServer.gracefulExit);
process.on('SIGINT', socketServer.gracefulExit);
if (process.argv[2] === '--daemon') {
if (fileExistsSync(`${__dirname}/pidfile`)) {
try {
const pid = fs.readFileSync(`${__dirname}/pidfile`, { encoding: 'utf-8' });
process.kill(pid, 0);
process.exit();
} catch (e) {
fs.unlinkSync(`${__dirname}/pidfile`);
}
}
fs.writeFile(`${__dirname}/pidfile`, process.pid);
}
function fileExistsSync(path) {
let exists = false;
try {
exists = fs.statSync(path);
} catch (err) {
exists = false;
}
return !!exists;
}