-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
96 lines (84 loc) · 2.79 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
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
// Node express program: app.js
// Parses www.mylaps.com/api json into p1lapchart json.
//
// To setup on AWS EC2 see:
// 1) http://iconof.com/blog/how-to-install-setup-node-js-on-amazon-aws-ec2-complete-guide/
// 2) https://gist.github.com/kentbrew/776580
// sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
var port = 8080;
var $ = require('jquery');
var express = require('express');
var app = express();
// http://stackoverflow.com/questions/13656300/jquery-getjson-doesnt-respond-but-direct-access-does
var allowCrossDomain = function (req, res, next) { // Allow (CORS) cross-domain requests
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
app.configure(function () {
app.use(allowCrossDomain);
});
function getSource(id) {
var source = "http://kenlin.com/x/p1lapchart/lapchart/2695656mylaps.json"; // http://www.mylaps.com/api/eventlapchart?id=2695656
if (id != null) {
source = 'http://www.mylaps.com/api/eventlapchart?id=' + id;
} else {
if (process.argv.length > 2) {
source = process.argv[2];
}
}
return source;
}
function enhance(data, source) {
// Add data.meta
data.p1meta = {
status: 0
,source: source
// ,createtime: new Date()
}
// Add data.laps by parsing data.lapchart.positions
data.p1laps = {};
for (var position=0; position<data.lapchart.positions.length; position++) {
for (var lap=0; lap<data.lapchart.laps.length; lap++) {
if (data.lapchart.positions[position][lap] != undefined) {
var startNumber = data.lapchart.positions[position][lap].startNumber;
if (data.lapchart.positions[position][lap] != undefined && startNumber != undefined && startNumber != "") {
if (data.p1laps[startNumber] == undefined) {
data.p1laps[startNumber] = [];
}
data.p1laps[startNumber][lap] = position+1;
}
}
}
}
// Delete properties from original mylaps.com JSON that we don't use
delete data.lapchart.laps;
delete data.lapchart.positions;
data.lapchart.participants.forEach(function(p) {
delete p.color;
});
return data;
};
cache = {}; // <id>: <json>
app.get('/api/eventlapchart/:id', function(req, res) {
var id = req.params.id;
var sourceurl = getSource(id);
if (cache[id] != null) {
console.log('getJSON("' + sourceurl + '") cached');
res.json(cache[id]);
} else {
$.getJSON(sourceurl, function(data) {
console.log('getJSON("' + sourceurl + '") success');
var json = enhance(data, sourceurl);
res.json(json);
cache[id] = json;
})
.fail(function(jqXJR, textStatus, errorThrown) {
console.log('getJSON("' + sourceurl + '") failed: ' + textStatus);
res.json({"p1meta": {"status": 404}});
});
}
});
app.listen(port);
console.log('Listening on port ' + port);