-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
253 lines (194 loc) · 6.79 KB
/
api.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Some sensitive info - Hate doing it like this but no choice due to how Heroku works :(
var private = process.env.twitterScreenName ? { twitter : {}, mongolab : {} } : require( './private/private.js' );
private.twitter.screen_name = private.twitter.screen_name || process.env.twitterScreenName;
private.mongolab.apiKey = private.mongolab.apiKey || process.env.mongoLabApiKey;
private.mongolab.uri = private.mongolab.uri || process.env.mongoLabUri;
// Required packages / files
var express = require( 'express' ),
mongodb = require( 'mongodb' );
// The api end points
var apis = {
"twitter" : {
"host" : "api.twitter.com",
"protocol" : "http",
"port" : 80
},
"delicious" : {
"host" : "feeds.delicious.com",
"protocol" : "http",
"port" : 80
},
"google" : {
"host" : "www.googleapis.com",
"protocol" : "https",
"port" : 443
}
};
var endpoints = {
"twitter" : {
"favourites" : "1/favorites/20645306.json",
"statuses" : "1/statuses/user_timeline.json?screen_name=theHuzz&exclude_replies=true&trim_user=true",
"friends" : "1/friends/ids.json?screen_name=theHuzz",
"user" : "1/users/show.json?screen_name=theHuzz"
},
"delicious" : {
"links" : "v2/json/djhuzz?count=100"
},
"google" : {
"bookshaveread" : "books/v1/users/101891936560271534706/bookshelves/4/volumes?country=GB&maxResults=40"
}
};
// Hold the collection in mongoDB I am using
var collection = 'robhuzzey';
var appHelpers = function( mongoDBconn, collection ) {
return {
conn : mongoDBconn,
getCache : function( options ) {
// If we have a connection, let's try and get the cache, otherswise drop right into error callback
if( this.conn ) {
var self = this;
self.conn.collection( collection, function( err, coll ) {
coll.findOne( { 'hash' : options.hash }, function( err, result ) {
if( result ) {
// If the record we found has expired, drop into error callback
// otherwise success
if( self.cacheExpired( result ) ) {
options.error( result );
console.log( 'uh oh!' );
} else {
options.success( result );
}
} else {
options.error( result );
}
});
});
} else {
options.error();
console.log( 'Unable to establish connection to mongoDB' );
}
},
saveCache : function( options ) {
if( this.conn ) {
// Save the cache
this.conn.collection( collection, function( err, coll ) {
var object_to_insert = { "hash" : options.hash, "data" : options.data, "created" : null, "modified" : null };
// Set the _id to our object_to_insert if we have one
if( options.mongoObj && options.mongoObj._id ) {
object_to_insert['_id'] = options.mongoObj._id;
object_to_insert['modified'] = new Date();
} else {
object_to_insert['created'] = new Date();
}
// Now save / update the object
coll.save( object_to_insert, { safe : true }, function( err ) {
console.log( 'saved cache' );
});
});
} else {
console.log( 'Unable to establish connection to mongoDB' );
}
},
cacheExpired : function( obj ) {
// Hold the lastmodified date that could be created date if new item
var lastModified = obj.modified ? obj.modified : obj.created;
// Setup the expiry date
var expires = lastModified;
expires.setHours( expires.getHours() + 1 ); // 1 hour expiry
console.log( 'Expiry Date: ' + expires );
var now = new Date();
console.log( 'Now: ' + now );
// Check if our expiry date is after now (returns bool)
return ( now > expires );
}
};
};
// Start off by defining the server
var server = express.createServer( express.logger() );
// Initialize the server
server.get( '*' , function( request, response ) {
// Quick fix to prevent the .ico file triggering this code
if( request.params[0] != '/favicon.ico' ) {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "X-Requested-With");
// Make a unique identifier for the request
var hash = require( 'crypto' ).createHash( 'md5' ).update( request.url ).digest( 'hex' );
// First up, see if we have a cache of this request already
mongodb.connect( private.mongolab.uri, function( err, conn ) {
// Init a new helper and try to get the cache
var helper = new appHelpers( conn, collection );
helper.getCache({
"hash" : hash,
"success" : function( data ) {
console.log( 'got from cache' );
response.send( data.data );
},
"error" : function( mongoObj ) {
// Break down the request url into parts we need
var url = request.url.match(/^\/(\w*)\/(.*)/);
var provider = ( url && url.length > 1 ) ? url[1] : null;
var endpoint = ( url && url.length > 1 ) ? url[2] : null;
var error = null;
if( apis[provider] === undefined || endpoints[provider] === undefined || endpoints[provider][endpoint] === undefined ) {
// List all potential routes
var routes = [];
for( var end in endpoints ) {
for( var point in endpoints[end] ) {
routes.push( 'get::/' + end + '/' + point );
}
}
response.send( 'No path matched, <br />Routes:<br />' + routes.join( '<br />' ) );
console.log( 'apis', apis );
console.log( 'provider', provider );
console.log( 'endpoint', endpoint );
} else {
// The options hash for the api request
var options = {
host: apis[provider].host,
port: apis[provider].port,
path: '/' + endpoints[provider][endpoint],
method: 'GET'
}
var data = '';
// The API request
var req = require( apis[provider].protocol ).request( options, function( res ) {
res.setEncoding( 'utf8' );
// Build up the response in data
res.on( 'data', function( chunk ) {
data += chunk;
});
// Once the api has finished, send output & save the cache
res.on( 'end', function() {
if( data ) {
// See if we have an error
error = JSON.parse( data ).error;
}
// If the data errors or is empty, return last know good state
if( !data || error ) {
console.log( 'The API returned an error : ' );
console.log( error );
console.log( 'Returning last known good state' );
response.send( mongoObj.data );
} else {
console.log( 'got from api' );
response.send( data );
helper.saveCache({
"hash" : hash,
"data" : data,
"mongoObj" : mongoObj
});
}
});
});
// End the request on the api
req.end();
}
}
});
});
}
});
var port = process.env.PORT || 5000;
server.listen( port, function() {
console.log( "Listening on " + port );
});