-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (52 loc) · 1.69 KB
/
app.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
const http = require('http'),
fs = require('fs'),
path = require('path'),
contentTypes = require('./utils/content-types'),
sysInfo = require('./utils/sys-info'),
env = process.env,
SocketIO = require('socket.io'),
Pool = require('./pool'),
Fetcher = require('./fetcher');
const server = http.createServer(function (req, res) {
let url = req.url;
if (url == '/') {
url += 'index.html';
}
// IMPORTANT: Your application HAS to respond to GET /health with status 200
// for OpenShift health monitoring
if (url == '/health') {
res.writeHead(200);
res.end();
} else if (url == '/info/gen' || url == '/info/poll') {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-cache, no-store');
res.end(JSON.stringify(sysInfo[url.slice(6)]()));
} else {
fs.readFile('./static' + url, function (err, data) {
if (err) {
res.writeHead(404);
res.end('Not found');
} else {
let ext = path.extname(url).slice(1);
res.setHeader('Content-Type', contentTypes[ext]);
if (ext === 'html') {
res.setHeader('Cache-Control', 'no-cache, no-store');
}
res.end(data);
}
});
}
});
// Memes Feed
const io = new SocketIO(server, {
origins: 'localhost:* tuzach.in:*'
});
const pool = new Pool(io);
const fetcher = new Fetcher(pool);
io.on('connection', function(socket) {
socket.emit('post', pool.get());
});
fetcher.start();
server.listen(env.NODE_PORT || 3000, env.NODE_IP || 'localhost', function () {
console.log(`Application worker ${process.pid} started...`);
});