-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·98 lines (85 loc) · 3.24 KB
/
server.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
//Generate unofficial rss feed and start a express server to serve them
//The web server staff was officialy based on the OpenShift template.
//Because that is where I want it to run.
var express = require('express');
var request = require('request');
var rss = require('./rssmaker');
var fs = require('fs');
var app = express();
var feedLoadInterval = 600000; //10 minutes
var feedList = {}; //List of feeds generated with their url in the app
var zcache = {}; //The cache use by the app
// Initialize the cache used by the app for the feeds data and pages
zcache['index.html'] = fs.readFileSync('./index.html');
// Function to get and set the cache
var zcacheGet = function(key) {
return zcache[key];
};
var zcacheSet = function(key, value) {
zcache[key] = value;
};
// Generates the rss feeds whose description is passed in parameter and stach them in the cache
var makeFeed = function(feeds) {
var feedname;
for ( var i=0; i < feeds.length; i++) {
feedname = feeds[i].name;
feedList[feedname] = "rss/" + feedname;
(function (feed) {
var options = {
url: feed.rssfeed.site_url,
headers: {
'User-Agent': 'request'
}
};
request(options, function(error, response, html){
if(!error) {
console.log('Feed generated : ' + feed.name);
var result = rss( html, feed.rssfeed, feed.rssmaker);
zcacheSet(feed.name, result);
} else {
console.log('Error generating feed for ' + feed.name);
zcacheSet(feed.name, ' something went wrong : ' + error);
}
});
})(feeds[i])
}
};
// Termination handler, terminate the server on specified signal.
var terminator = function(sig){
if (typeof sig === "string") {
console.log('%s: Received %s - terminating Remora.', Date(Date.now()), sig);
process.exit(1);
}
};
// Setup termination handlers (for exit and a list of signals).
var setupTerminationHandlers = function(){
// Process on exit and signals.
process.on('exit', function() { terminator(); });
// Removed 'SIGPIPE' from the list - bugz 852598.
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach(function(element, index, array) {
process.on(element, function() { terminator(element); });
});
};
// To pass the feeds description to the server
app.setFeed = function (feedsDetail) {
setInterval(makeFeed(feedsDetail), feedLoadInterval);
}
// The route to get one of the feed generated
app.get('/rss/:feedname', function(req, res) {
res.send(zcacheGet(req.params.feedname));
});
// Main route
app.get('/', function(req, res) {
res.setHeader('Content-Type', 'text/html');
res.send(zcacheGet('index.html') );
});
//Return the feed list, called by the main page in ajax
app.get('/feedlist', function(req, res) {
res.setHeader('Content-Type', 'text/json');
res.send(JSON.stringify(feedList));
});
// Set up static path for the server
app.use(express.static('static'));
module.exports = app;