-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·365 lines (316 loc) · 12.9 KB
/
server.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// Deploy/server.js - This is the heart of the server. Most all the work gets done in here.
//
//var PORT = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var PORT = 8080;
var IPADDRESS = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var MONGOIP = process.env.OPENSHIFT_MONGODB_DB_HOST || '127.0.0.1';
var MONGOPORT = process.env.OPENSHIFT_MONGODB_DB_PORT || 27017;
// Adding in for New Relic logging
require('newrelic');
var mongoose = require ("mongoose");
var restify = require ("restify");
var Guid = require ("guid");
// Schemas
var feedHistorySchema = new mongoose.Schema({
feedid: { type: String },
lastDispatch: { type: Number, min: 0 },
lastUpdate: { type: Number, min: 0 },
lastUpdatedBy: { type: String },
lastSuccess: { type: Number, min: 0 },
});
var feedSchema = new mongoose.Schema({
internalID: { type: String },
title: { type: String, trim: true },
url: { type: String, trim: true },
feedid: { type: String },
category: { type: Number, min: 0 },
type: { type: Number, min: 0 },
image: { type: String, trim: true },
timeOffset: { type: Number, min: 0},
who: { type: Number, min: 0 },
personal: { type: Number, min: 0 },
dateAdded: { type: Number, min: 0 },
lastDispatch: { type: Number, min: 0 },
lastUpdate: { type: Number, min: 0 },
lastUpdatedBy: { type: String },
lastSuccess: { type: Number, min: 0 },
});
var feedDataSchema = new mongoose.Schema({
feedid: { type: String },
title: { type: String },
url: { type: String },
image: { type: String },
pubdateseconds: { type: Number, min: 0 },
timeaggregated: { type: Number, min: 0 },
title: { type: String },
description: { type: String },
guid: { type: String },
uuid: { type: String },
author: { type: String },
category: { type: String },
});
var textSearch = require('mongoose-text-search');
feedDataSchema.plugin(textSearch);
feedDataSchema.index({ title: 'text'});
var feedSubscriptionSchema = new mongoose.Schema({
uid: { type: String, min: 0},
feeid: { type: Number, min: 0}
});
var feedUserSchema = new mongoose.Schema({
uid: { type: Number, min: 0},
lastLogin: { type: Number, min: 0},
// access: { type: Number, min: 0}, //Not sure this is good to have here... security wise. or if it matters.
timezone: { type: Number, min: 0},
SPP: { type: Number, min: 0},
lastDisplay: { type: Number, min: 0},
GPS: { type: String},
});
// Get environment currently running under
var env = "live";
var feeds = mongoose.model('Feeds', feedSchema);
var history = mongoose.model('History', feedHistorySchema);
var feedData = mongoose.model('FeedData', feedDataSchema);
var feedSubs = mongoose.model('FeedSubscriptions', feedSubscriptionSchema);
var feedUsers = mongoose.model('FeedUsers', feedUserSchema);
function respond(req, res, next) {
//console.log(req.params);
/* if (req.params.server === undefined) {
return next(new restify.InvalidArgumentError('Server must be supplied'))
}*/
var options = {upsert: true};
// Not sure if I have all this data or not for this call.
var feedData = {
title: req.params.title,
url: req.params.url,
feedid: req.params.feedid,
category: req.params.category,
type: req.params.type,
image: req.params.image,
timeOffset: req.params.timeOffset,
who: req.params.who,
personal: req.params.personal,
dateAdded: req.params.dateAdded,
};
// Saving it to the database.
feeds.findOneAndUpdate({ feedid: req.params.feedid, url: req.params.url }, feedData, options, function (err) {
if (err) {console.log ('Error on save for '+req.params+" Error: "+err)} else {
res.send('OK');
}
});
}
// Only ADD at the moment.
function addFeedGUID(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
console.log(req.params);
/* if (req.params.server === undefined) {
return next(new restify.InvalidArgumentError('Server must be supplied'))
}*/
var options = {upsert: true};
var guidData = Guid.create();
var guid = guidData.value;
// Not sure if I have all this data or not for this call.
var feedData = {
title: req.params.title,
url: req.params.url,
feedid: guid,
category: req.params.category,
type: req.params.type,
image: req.params.image,
timeOffset: req.params.timeOffset,
who: req.params.who,
personal: req.params.personal,
dateAdded: req.params.dateAdded,
};
console.log(feedData);
// Saving it to the database.
feeds.findOneAndUpdate({ feedid: req.params.feedid, url: req.params.url }, feedData, options, function (err) {
if (err) {console.log ('Error on save for '+req.params+" Error: "+err)} else {
res.send('OK');
}
});
}
function findoldest(server) {
return deployment.aggregate({key: {"lastUpdate":-1},reduce: function (curr,result) {result.total++; if(curr.datestamp<result.datestamp) { result.datestamp=curr.datestamp;} }, initial: {datestamp: Date.now()} });
}
function feedList(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
feeds.find({},{feedid: 1, title: 1},function(err,data) {
if (err) { res.send(err); } else { res.send(data); }
});
}
function feedInfo(req, res, next) {
var lasttwelve=Date.now()-43200000000;
feedData.find({ timeaggregated: { $gt: lasttwelve }, feedid: req.params.feedid }, null, function(err,data) {
if (err) { res.send(err); } else { res.send(data) }
});
}
function currentStories(req, res, next) {
// > db.feeddatas.find({ timeaggregated: { $gt: 1391456885107000 }}).count(); => 24
//Date.now() == 1391369704,609898 1391456086000000-7200000
//Twelve Hours: var lasttwelve=Date.now()-43200000;
//Six Hours: var lasttwelve=Date.now()-21600000;
// var lasttwelve=Date.now()-7200000;
// var lasttwelve=(Date.now()-2880000)*1000;
var lasttwelve=(Date.now()-7200000)*1000;
feedData.find({ timeaggregated: { $gt: lasttwelve } }, { feedid:1, image:1, category:1, title: 1, description:1, uuid:1, pubdateseconds: 1}, { sort: { timeaggregated: -1}},function(err,data) {
if (err) { res.send(err); } else { res.send(data) }
});
}
// We need to rewrite dispatch so that it looks at the updateData instead of at the feed listings.
// The catch with this is that a new updateData record won't be created because one doesn't exist... so we need to create them,
// or we need to store the last update time and last dispatch time in the feeds table in the first place.
function dispatch(req, res, next) {
feeds.findOne({ "lastDispatch" : { "$exists" : false } },function(err,data) {
if (data!=null) {
data.lastDispatch=0;
data.save(function (err) { if (err) { res.send(err); } });
}
});
//This can't have feedid come in as the server doesn't know what ID it'll get
feeds.findOne({ }, null, { sort: { lastDispatch: 1 } }, function(err,data) { //, null, { sort: { lastUpdate: -1 } }, function(err,data) {
if (err) { res.send(err); } else {
var options = {upsert: true};
var updateData = {
lastDispatch: Date.now(),
// lastUpdate: Date.now(),
lastUpdatedBy: req.params.server,
// lastSuccess: { type: Number, min: 0 },
};
var lasttwelve=Date.now()-43200000000;
//console.log(lasttwelve);
feedData.find({ timeaggregated: { $gt: lasttwelve }, feedid: data.feedid }, { uuid: 1 }, function (err,udata) {
//console.log(udata);
var d = {
uuids: udata,
info: data,
};
res.send(d);
});
feeds.findOneAndUpdate({ feedid: data.feedid }, updateData, options, function (err) { if (err) { res.send(err); } });
}
});
}
//// Feed Story Stuff ////////////////////////////////////////////////////////////////////////////////
function getStoryData(req, res, next) {
feedData.findOne({ uuid: req.params.uuid }, function (err,data) { if (err) { res.send(err); } else { res.send(data); } });
}
function searchStoryData(req, res, next) {
feedData.textSearch(req.params.search,function (err,data) {
if (err) { res.send(err); } else { res.send(data); }
});
}
function addStoryData(req, res, next) {
//console.log(req.params);
/* if (req.params.server === undefined) {
return next(new restify.InvalidArgumentError('Server must be supplied'))
}*/
var options = {upsert: true};
var storyData = {
feedid: req.params.feedid,
title: req.params.title,
url: req.params.url,
image: req.params.image,
pubdateseconds: req.params.pubdateseconds,
timeaggregated: req.params.timeaggregated,
category: req.params.category,
description: req.params.description,
author: req.params.author,
uuid: req.params.uuid,
guid: req.params.guid,
};
// Saving it to the database.
feedData.findOneAndUpdate({ feedid: req.params.feedid, uuid: req.params.uuid }, storyData, options, function (err,data) {
if (err) {console.log ('Error on save for '+req.params+" Error: "+err)} else {
var updateData = {
lastUpdate: Date.now(),
lastSuccess: Date.now(),
};
//res.send(data);
feeds.findOneAndUpdate({ feedid: data.feedid }, updateData, options, function (err) { if (err) { res.send(err); } });
res.send('OK');
}
});
}
function findURL(req, res, next) {
feedData.findOneAndUpdate({ uuid: req.params.uuid }, {$inc: {hits: 1}}, { url: 1 }, function (err,data) {
if (err) { console.log('Error Looking Up uuid: '+req.params.uuid); } else {
res.send(data); } });
}
function urldecode(url) {
return decodeURIComponent(url.replace(/\+/g, ' '));
}
function shortener(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
feedData.findOne({uuid: req.params.uuid}, {url:1}, function(err,data) {
res.writeHead(302, {
'Location': urldecode(data.url)
});
res.end();
});
}
///// Feed Subscription Stuff ////////////////////////////////////////////////////////////////////////
function addFeedSubscription(req, res, next) {
var options = {upsert: true};
var sub = { feedid: req.params.feedid, uid: req.params.uid };
console.log(sub);
feedSubs.findOneAndUpdate({uid: req.params.uid, feedid: req.params.feedid}, sub, options, function(err,data) {
if (err) { console.log ('Error saving feed subscription '+err); } else { res.send("OK"); }
});
}
function getFeedSubscriptions(req, res, next) {
feedSubs.find({},null,function (err,data) { if (err) { res.send(err); } else { res.send(data); }});
}
function getFeedSubscriptionsPerUser(req, res, next) {
feedSubs.find({uid: req.params.uid},null,function (err,data) { if (err) { res.send(err); } else { res.send(data); }});
}
///// User Info Stuff ////////////////////////////////////////////////////////////////////////////////
function addFeedUser(req, res, next) {
var options = {upsert: true};
var user = { uid: req.params.uid, lastLogin: req.params.lastLogin, timezone: req.params.timezone, SPP: req.params.spp, lastDisplay: req.params.ldisplay, GPS: req.params.gps };
console.log(user);
feedUsers.findOneAndUpdate({uid: req.params.uid }, sub, options, function(err,data) {
if (err) { console.log ('Error saving feed subscription '+err); } else { res.send("OK"); }
});
}
function getFeedUser(req, res, next) {
feedUsers.find({uid : req.params.uid },null,function (err,data) { if (err) { res.send(err); } else { res.send(data); }});
}
var server = restify.createServer();
server.use(restify.bodyParser());
server.get('/hello/:name',function(req, res, next) { res.send("Hey, "+req.params.name+". We're in the pipe, 5 by 5"); });
server.post('/feed/:feedid',respond);
server.get('/feed/:feedid',feedInfo);
server.post('/addfeed/',addFeedGUID);
server.post('/story/:uuid',addStoryData);
server.get('/story/:uuid',getStoryData);
server.get('/dispatch/:server',dispatch);
server.get('/feedlist/',feedList);
server.get('/feedlatest/:feedid',feedInfo);
server.get('/current/',currentStories);
server.get('/short/:uuid',findURL);
server.get('/s/:uuid',shortener);
server.post('/setfs/',addFeedSubscription);
server.get('/getfs/',getFeedSubscriptions);
server.get('/getfs/:uid',getFeedSubscriptionsPerUser);
// This should really have auth around it. Not that it stores passwords.
server.post('/setui/:uid',addFeedUser);
server.get('/getui/:uid',getFeedUser);
// Search
server.get('/search/:query',searchStoryData);
// Here we find an appropriate database to connect to, defaulting to
// localhost if we don't find one.
var uristring = 'mongodb://inator:jiHU*gy@'+MONGOIP+':'+MONGOPORT+'/feedinator';
// Makes connection asynchronously. Mongoose will queue up database
// operations and release them when the connection is complete.
mongoose.connect(uristring, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + uristring + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + uristring);
// Deployment Schema setup, Mongo connected, time to start listening
server.listen(PORT, IPADDRESS, function() {
console.log('%s listening at %s', server.name, server.url);
});
}
});