-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (66 loc) · 1.81 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
var
express = require('express'),
http = require('http'),
request = require('request'),
csv = require('csv'),
fs = require('fs'),
async = require('async'),
util = require('util'),
// Data providers.
mapping = require('./provider/mapping.js'),
ogd_static = require('./provider/ogd_static.js'),
wl_live = require('./provider/wl_live.js'),
merged_data = require('./provider/merged_data.js');
var app = express();
// Update static providers in parallel, once in a while.
function timeStatic(callback) {
async.parallel([
mapping.update,
ogd_static.update
],
function(err) {
if (!err) {
if (callback) { callback(); }
setTimeout(timeStatic, 10 * 60 * 1000); // Every 10min.
}
}
)
}
// Update live data, then merged data in series, often.
function timeLive(callback) {
async.series([
wl_live.update,
function(callback) {
merged_data.update(mapping, ogd_static, wl_live, callback);
}
],
function(err) {
if (callback) { callback(); }
setTimeout(timeLive, 2 * 1000); // Every 2sec.
}
)
}
// Start timers in series (make sure, static data is available before processing live data).
async.series([
timeStatic,
timeLive,
],
function(err) {
if (!err) {
// Data is available, change response function.
responseFunction = function(req, res, next) {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(JSON.stringify(merged_data.data));
};
}
}
);
// Default response: data not ready, override when ready.
var responseFunction = function(req, res, next) {
res.writeHead(503, { });
res.end("data not ready");
};
app.get('/', function(req, res, next) {
responseFunction(req, res, next);
});
app.listen(8080);