-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
89 lines (83 loc) · 2.52 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
var express = require('express'),
app = express(),
request = require('request'),
drupalUrl = 'http://dev.bariswanschers.com:8080',
redis = require('redis'),
client = redis.createClient();
dust = require('./dist/index.js'),
bodyParser = require('body-parser');
app.use(bodyParser.json());
var helpers = {
key_exists : function (key,callback) {
client.exists(key, function (err, doesExist) {
switch(doesExist){
case 0: // the key does not exist
callback(false, {});
break;
case 1: // the key exists
client.hgetall(key, function (err, obj){
callback(true, obj);
});
break
}
});
},
key_set : function (key, data, callback) {
client.hmset(key, data, callback);
}
};
app.get('/node/:nid', function(req, res){
var options = {
'url' : drupalUrl + req.params.nid,
'json' : true
};
helpers.key_exists('node:'+req.params.nid, function(status, data) {
if (!status) {
request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
dust.render('index.dust', body, function(err, html_out) {
helpers.key_set('node:'+req.params.nid,{'body' : JSON.stringify(body), 'html' : html_out}, function(){
res.send(html_out);
client.expire('node:'+req.params.nid, 60);
})
});
}
});
}
else {
res.send(data.html);
}
});
});
app.get('/nodes', function(req, res){
var options = {
'url' : drupalUrl,
'json' : true
};
helpers.key_exists('node:'+req.params.nid, function(status, data) {
if (!status) {
request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
dust.render('index.dust', body, function(err, html_out) {
helpers.key_set('node:'+req.params.nid,{'body' : JSON.stringify(body), 'html' : html_out}, function(){
res.send(html_out);
client.expire('node:'+req.params.nid, 60);
})
});
}
});
}
else {
res.send(data.html);
}
});
});
app.post('/cacheclear', function(req, res){
if (req.body.keys.length > 0){
for (var i = 0; i < req.body.keys.length; i++){
client.del(req.body.keys[i]);
}
}
res.status(200).end();
});
app.listen(3000);