forked from soulkobk/DiscordBot_GameStatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (90 loc) · 3.12 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
94
95
96
97
98
99
100
101
102
/*
Author: Ramzi Sah#2992
Desription:
creates multiple instances of the bot
Updated:
20220403 - soulkobk, updated pathing on web server
*/
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
// read configs
const fs = require('fs');
const config = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8'));
// create temp data folders
if (!fs.existsSync(__dirname + "/temp")){
fs.mkdirSync(__dirname + "/temp");
};
if (!fs.existsSync(__dirname + "/temp/graphs")){
fs.mkdirSync(__dirname + "/temp/graphs");
};
if (!fs.existsSync(__dirname + "/temp/data")){
fs.mkdirSync(__dirname + "/temp/data");
};
// initiation
const ChildProcess = require('child_process');
var instances = [];
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
// start instances
for (let i = 0; i < config["instances"].length; i++) {
// create child process for every instance
let instance = ChildProcess.fork(__dirname + '/bot.js');
instance.on('message', function(m) {
console.log('[instance ' + m.instanceid + ']:', m.message);
});
// communicate id to instence
instance.send({id: i});
// push to instances list
instances.push(instance);
};
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
// start web server
const http = require('http');
const path = require('path');
var dir = "./temp/graphs";
dir = path.resolve(dir);
var mime = {
html: 'text/html',
txt: 'text/plain',
css: 'text/css',
gif: 'image/gif',
jpg: 'image/jpeg',
png: 'image/png',
svg: 'image/svg+xml',
js: 'application/javascript'
};
var server = http.createServer(function (req, res) {
var reqpath = req.url.toString().split('?')[0];
if (req.method !== 'GET') {
res.statusCode = 501;
res.setHeader('Content-Type', 'text/plain');
return res.end('Method not implemented');
}
var file = path.join(dir, reqpath.replace(/\/$/, '/index.html'));
if (file.indexOf(dir + path.sep) !== 0) {
res.statusCode = 403;
res.setHeader('Content-Type', 'text/plain');
return res.end('Forbidden');
}
var type = mime[path.extname(file).slice(1)] || 'text/plain';
var s = fs.createReadStream(file);
s.on('open', function () {
res.setHeader('Content-Type', type);
s.pipe(res);
});
s.on('error', function () {
res.setHeader('Content-Type', 'text/plain');
res.statusCode = 404;
res.end('Not found');
});
});
// start web server
try {
server.listen(config["webServerPort"], function () {
console.log('Started web server ' + config["webServerHost"] + ":" + config["webServerPort"]);
});
} catch (error) {
// console.error(error);
console.log("PORT " + config["webServerPort"] + "ALREADY IN USE !");
};