forked from LocalData/localdata-api
/
web.js
351 lines (312 loc) · 9.21 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
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
/*jslint node: true */
'use strict';
var http = require('http');
var express = require('express');
var mongo = require('mongodb');
var uuid = require('node-uuid');
var fs = require('fs');
var s3 = require('connect-s3');
/*
* Routes are split into separate modules.
*/
var forms = require('./forms');
var responses = require('./responses');
var collectors = require('./collectors');
var surveys = require('./surveys');
var scans = require('./scans');
var parcels = require('./parcels');
var RESPONSES = 'responseCollection';
var FORMS = 'formCollection';
var COLLECTORS = 'collectorCollection';
var SURVEYS = 'surveyCollection';
var SCANIMAGES = 'scanCollection';
var server;
var app = express(express.logger());
var db;
// IE 8 and 9 can't post application/json for cross-origin requests, so we
// accept text/plain treat it as JSON.
// TODO: if we need to accept text/plain in the future, then we need to adjust
// this
function textParser(req, res, next) {
if (req._body) { return next(); }
req.body = req.body || {};
// For GET/HEAD, there's no body to parse.
if ('GET' === req.method || 'HEAD' === req.method) { return next(); }
// Check for text/plain content type
var type = req.headers['content-type'];
if (type === undefined || 'text/plain' !== type.split(';')[0]) { return next(); }
// Flag as parsed
req._body = true;
console.log('Got text/plain');
// Parse as JSON
var buf = '';
req.setEncoding('utf8');
req.on('data', function(chunk){
buf += chunk;
});
req.on('end', function(){
try {
if (!buf.length) {
req.body = {};
} else {
req.body = JSON.parse(buf);
}
next();
} catch (err) {
err.status = 400;
next(err);
}
});
}
app.set('jsonp callback', true);
app.use(express.methodOverride());
app.use(textParser);
// Default to text/plain if no content-type was provided.
app.use(function(req, res, next) {
if (req.body) { return next(); }
if ('GET' === req.method || 'HEAD' === req.method) { return next(); }
if (!req.headers['content-type']) {
req.body = {};
textParser(req, res, next);
} else {
next();
}
});
app.use(express.bodyParser());
// Add common headers
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Mime-Type, X-Requested-With, X-File-Name, Content-Type");
next();
});
// ID generator
var idgen = uuid.v1;
// Local static files
function sendFile(response, filename, type) {
fs.readFile('static/' + filename, function(err, data) {
if (err) {
console.log(err.message);
response.send(500);
return;
}
response.header('Content-Type', type);
response.send(data);
});
}
// Set up routes
function setupRoutes(db, settings) {
forms.setup(app, db, idgen, FORMS);
responses.setup(app, db, idgen, RESPONSES);
collectors.setup(app, db, idgen, COLLECTORS);
surveys.setup(app, db, idgen, SURVEYS);
scans.setup(app, db, idgen, SCANIMAGES, settings);
parcels.setup(app, settings);
// Mobile collection app
app.use(s3({
pathPrefix: '/mobile',
remotePrefix: settings.mobilePrefix
}));
// Ringleader's administration/dashboard app
app.use(s3({
pathPrefix: '/',
remotePrefix: settings.adminPrefix
}));
// Internal operational management app
// TODO: move this to S3
var opsPrefix = '/ops';
app.use(function (req, res, next) {
var path;
var url = req.url;
if (url.length < opsPrefix.length ||
url.substr(0, opsPrefix.length) !== opsPrefix) {
return next();
}
path = url.substr(opsPrefix.length);
if (path === '' || path === '/') {
res.redirect('/static/surveys.html');
res.statusCode = 302;
res.setHeader('Location', req.url + '/');
res.end();
} else {
var index = path.lastIndexOf('.');
var format = '';
if (index > -1) {
format = path.substring(index);
}
var type;
switch (format) {
case '.html':
type = 'text/html';
break;
case '.css':
type = 'text/css';
break;
case '.js':
type = 'application/javascript';
break;
case '.gif':
type = 'image/gif';
break;
case '.png':
type = 'image/png';
break;
default:
type = 'text/html';
}
sendFile(res, path, type);
}
});
}
// Ensure certain database structure.
function ensureStructure(db, callback) {
// Map f(callback) to f(error, callback)
// If we encounter an error, bail early with the callback.
function upgrade(g) {
return function (err, done) {
if (err) { done(err); }
g(done);
};
}
// Chain async function calls
function chain(arr, done) {
return arr.reduceRight(function (memo, f, index) {
return function (err) {
upgrade(f)(err, memo);
};
}, function (e) { done(e); });
}
function ensureResponses(done) {
db.collection(RESPONSES, function (error, collection) {
if (error) { return done(error); }
chain([function indexCentroid(done) {
// Ensure we have a geo index on the centroid field.
collection.ensureIndex({'geo_info.centroid': '2d'}, done);
},
function indexCreated(done) {
// Index the creation date, which we use to sort
collection.ensureIndex('created', done);
},
function indexParcelId(done) {
// Index the parcel ID
collection.ensureIndex('parcel_id', done);
}], done)();
});
}
function ensureForms(done) {
db.collection(FORMS, function (error, collection) {
if (error) { return done(error); }
chain([function indexCreated(done) {
// Index the creation date, which we use to sort
collection.ensureIndex('created', done);
},
function indexParcelId(done) {
// Index the parcel IDs, used by paper forms
collection.ensureIndex('parcels.parcel_id', done);
}], done)();
});
}
function ensureSurveys(done) {
db.collection(SURVEYS, function (error, collection) {
if (error) { done(error); }
chain([function indexSlug(done) {
// Index the slug field.
collection.ensureIndex('slug', done);
},
function indexId(done) {
// Index the survey ID.
collection.ensureIndex('id', done);
}], done)();
});
}
function ensureSlugs(done) {
db.collection(SURVEYS, function (error, collection) {
// Look for surveys with no slug
collection.find({}, function(err, cursor) {
cursor.toArray(function (err, arr) {
if (err) { return done(err); }
var count = 0;
arr.forEach(function (item) {
if (item.slug === undefined) {
// Add a slug
surveys.checkSlug(collection, item.name, 0, function (err, slug) {
if (err) { return done(err); }
// Update entry
collection.update({_id: item._id}, {'$set': {slug: slug}}, function (error) {
if (err) { return done(err); }
count += 1;
if (count === arr.length) {
done();
}
});
});
} else {
count += 1;
if (count === arr.length) {
done();
}
}
});
});
});
});
}
chain([ensureResponses, ensureForms, ensureSurveys, ensureSlugs], callback)();
}
function startServer(port, cb) {
server = http.createServer(app);
server.listen(port, function (err) {
console.log('Listening on ' + port);
if (cb !== undefined) { cb(err); }
});
}
function run(settings, cb) {
if (!db) {
console.log('Using the following settings:');
console.log('Port: ' + settings.port);
console.log('Mongo host: ' + settings.mongo_host);
console.log('Mongo port: ' + settings.mongo_port);
console.log('Mongo db: ' + settings.mongo_db);
console.log('Mongo user: ' + settings.mongo_user);
console.log('Postgresql host: ' + settings.psqlHost);
console.log('Postgresql db: ' + settings.psqlName);
console.log('Postgresql user: ' + settings.psqlUser);
// Set up database
db = new mongo.Db(settings.mongo_db, new mongo.Server(settings.mongo_host,
settings.mongo_port,
{}), {});
setupRoutes(db, settings);
}
// Kick things off
db.open(function() {
if (settings.mongo_user !== undefined) {
db.authenticate(settings.mongo_user, settings.mongo_password, function(err, result) {
if (err) {
console.log(err.message);
return cb(err);
}
ensureStructure(db, function (error) {
if (error) { throw error; }
startServer(settings.port, cb);
});
});
} else {
ensureStructure(db, function (error) {
if (error) { throw error; }
startServer(settings.port, cb);
});
}
});
}
function stop() {
server.close();
db.close();
console.log('Stopped server');
}
module.exports = {
run: run,
stop: stop
};
// If this was run directly, run!
if (require.main === module) {
run(require('./settings.js'));
}