-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
51 lines (41 loc) · 1.49 KB
/
service.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
module.exports = function(routes, db) {
var cache = require('./cache')(db);
var client = require('./client');
return {
call : function(route, request, callback) {
routes.getParams(request.path, route, function(err, params) {
if (err) {
return callback(err);
}
var id = params[route.resource.id] || route.resource.id;
var resource = route.resource.name;
console.log('ID:', id);
console.log('RESOURCE:', resource);
cache.get(resource, id, function(err, data) {
if (data) {
console.log('Data found in cache', data);
return callback(null, data);
}
console.log('Data not found in cache');
if (err || !data) {
client.get(route.out, params, function(err, result) {
if (err) {
console.log('Error while getting data from remote service');
return callback(err);
}
console.log('Data received from remote service', result);
// TODO why not send back directly the answer and store in parallel?
cache.put(resource, id, result, function(err, saved) {
if (err) {
console.log('Can not put in cache, send back response directly', data);
return callback(null, result);
}
return callback(null, saved.ops[0].data);
});
});
}
});
});
}
};
};