This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
/
web.js
115 lines (98 loc) · 3.83 KB
/
web.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
var http = require("http")
,fs = require('fs')
,querystring = require('querystring')
,connect = require("connect")
,ejs = require("ejs")
,port = process.env.PORT || 3000
,sparql_api_key = "368389391f29f9442406b400a42e1dfd6eaacb22"
,sparql_host = "iati-ld.tklapp.com"
,sparql_path = "/r4d/sparql/"
,redis_cache_ttl = 1
,redis_cache_prefix = "r4dProjectCache:"
,sparql
,redis
,rtg;
if (process.env.REDISTOGO_URL) {
// get redistogo on heroku
rtg = require("url").parse(process.env.REDISTOGO_URL);
redis = require("redis").createClient(rtg.port, rtg.hostname);
redis.auth(rtg.auth.split(":")[1]);
} else {
// Get localhost redis
redis = require('redis').createClient();
}
/**
* Precompile our sparql request template and store it
*/
fs.readFile('sparql.ejs', 'utf8', function(err, data) {
if(!err) {
sparql = data;
}
});
connect(
connect.logger(),
connect.static(__dirname + "/public"),
// create a router to handle application paths
connect.router(function(app) {
app.get("/project/:prid", function(req, res) {
var cached,
squery,
query,
opts,
req_params = querystring.parse(req.url.split("?")[1]);
function respond(data) {
//accept jsonp requests
if (req_params.callback) {
// if we have a callback function name, do JSONP
data = req_params.callback + "(" + data + ");";
}
// write the results to the output
res.writeHead(200, {
// change MIME type to JSON
"Content-Type": (req_params.callback) ? "application/javascript" :"application/json"
});
res.end(data);
}
// Look for a copy in cache
redis.get(redis_cache_prefix + req.params.prid, function(err, data){
if (data) {
console.log('responding with cached data');
respond(data);
} else {
squery = encodeURIComponent(ejs.render(sparql, {
prid : req.params.prid
})).replace(/%20/g, '+');
query = "output=json&query=";
opts = {
host : sparql_host,
path : sparql_path + '?' + query + squery,
port : "80"
};
http.get(opts, function(response){
var results = '';
response.on('data', function(chunk) {
results += chunk;
});
response.on('end', function(){
console.log('responding with fresh data');
respond(results);
// Write to the cache
console.log('writing to cache');
redis.set(redis_cache_prefix + req.params.prid, results, function(){
console.log('setting expiry');
redis.expire(redis_cache_prefix + req.params.prid, redis_cache_ttl, function(err, ok){
if (ok) {
console.log('Expiry set ok');
} else {
console.log('Expiry set failed:' + err);
}
});
});
});
});
}
});
});
})
).listen(port);
console.log('r4d server running on port ' + port);