-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
176 lines (166 loc) · 5.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const http = require('http');
const { spawn } = require('child_process');
const fs = require('fs');
var curlTestsDefinitions = {};
var PORT = 8080;
var interval = 10000;
var command = 'curl';
var testNames = [];
try { //open config file
const configfile = fs.readFileSync("logger.conf").toString();
curlTestsDefinitions = JSON.parse(configfile);
PORT = curlTestsDefinitions["PORT"];
interval = curlTestsDefinitions["INTERVAL"];
command = curlTestsDefinitions["COMMAND"];
} catch (error) {
throw 'ERROR when trying to open and parse the config file.'
}
class curlTest {
testsLog = [];
constructor(testName, test, expected) {
this.testName = testName;
this.test = test;
this.expected = expected;
console.log(`${this.testName} is active`);
this.performTest();
var self = this;
var curlInterval = setInterval(function () {
console.log(`Interval reached every ${interval}ms`);
self.performTest();
}, interval);
}
performTest() {
const curlTest = spawn(command, this.test);
curlTest.stdout.on('data', (data) => {
//console.log(`stdout: ${data}`);
var result = this.processData(data);
var toLog = ([Date.now(), result]);
//console.log(toLog);
this.storeLog(toLog);
});
curlTest.stderr.on('data', (data) => {
//console.log('curl error:' + data)
this.errorLog([Date.now(), 'stderr', data.toString()]);
var result = this.processData(data);
var toLog = ([Date.now(), result]);
this.storeLog(toLog);
})
console.log(`A test has been performed for ${this.testName}.`);
}
processData(data) { //returns a number: 0=down, 1=up, 2=test was partially succesful, 3=mysterious error
const str = data.toString('utf8');
var match = false; //turns true when found a match
var mismatch = false; //turns true when didn't find a match
for (key in this.expected) {
if (str.indexOf(this.expected[key]) != -1) {
match = true;
} else {
mismatch = true;
}
}
if (match == false && mismatch == true) {
//completely wrong data
this.errorLog([Date.now(), "badResponse", str]);
return 0
} else if (match == true && mismatch == true) {
//some test passed, some did not
this.errorLog([Date.now(), "partialResponse", str]);
return 2
} else if (match == true && mismatch == false) {
//all tests passed! congratulations!
return 1
} else if (match == false && mismatch == false) {
//error, this shouldn't happen
this.errorLog([Date.now(), "horribleError", str]);
return 3
};
};
storeLog(data) {
let fd;
try {
fd = fs.openSync(`${this.testName}.log`, 'a');
fs.appendFileSync(fd, JSON.stringify(data) + '\r\n', 'utf8');
} catch (err) {
console.error('storeLog error.')
} finally {
if (fd !== undefined)
fs.closeSync(fd);
}
console.log(`Logged the test for ${this.testName}`);
}
errorLog(data) {
let fd;
try {
fd = fs.openSync(`${this.testName}-errors.log`, 'a');
fs.appendFileSync(fd, JSON.stringify(data) + '\r\n', 'utf8');
} catch (err) {
console.error('error when logging an error')
} finally {
if (fd !== undefined)
fs.closeSync(fd);
}
console.log(`Logged an error for ${this.testName}`);
}
};
for (key in curlTestsDefinitions) { //create the objects
if (key.indexOf("Test") != -1) {
const test = curlTestsDefinitions[key]; // array of arguments for curl
const testName = key.slice(0, key.indexOf("Test")); //name of the test
const expected = curlTestsDefinitions[`${testName}Expected`]; //array of strings that should be found in the output of curl.
eval(`var ${key} = new curlTest(testName, test, expected)`); //create curlTest object with the name it was given in the definitions array.
testNames.push(testName);
}
}
function requestListener(req, res) {
if (req.url === "/") {
fs.readFile('index.html', function (err, data) {
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(data);
})
} else if (req.url === "/style.css") {
fs.readFile('style.css', function (err, data) {
res.writeHead(200, { "Content-Type": "text/css; charset=utf-8" });
res.end(data);
})
} else if (req.url === "/script.js") {
fs.readFile('script.js', function (err, data) {
res.writeHead(200, { "Content-Type": "text/javascript; charset=utf-8" });
res.end(data);
})
} else if (req.url === "/favicon.ico") {
fs.readFile('favicon.ico', function (err, data) {
res.writeHead(200, { "Content-Type": "image/x-icon" });
res.end(data);
})
} else if (req.url === "/lmsans10-regular.otf") {
fs.readFile('lmsans10-regular.otf', function (err, data) {
res.writeHead(200, { "Content-Type": "font/otf" });
res.end(data);
})
} else if (req.url === "/logger.conf") {
fs.readFile('logger.conf', function (err, data) {
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
res.end(data);
})
} else if (req.url === "/tests.json") {
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
res.end(JSON.stringify(testNames));
} else if (req.url.slice(req.url.length - 4, req.url.length) === ".log") {
const reqLog = req.url.slice(1, req.url.length - 4);
if (testNames.indexOf(reqLog) != -1) {
fs.readFile(`${reqLog}.log`, function (err, data) {
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
res.end(data);
})
} else {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end('Log not found');
}
} else {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end('Not found');
}
}
const server = http.createServer(requestListener);
server.listen(PORT);
console.log(`listening on port: ${PORT}`);