Created
June 26, 2011 07:57
-
-
Save deoxxa/1047384 to your computer and use it in GitHub Desktop.
Odd behaviour with mongodb-native
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| console.warn(); | |
| console.warn('=============================='); | |
| console.warn(' Buffered Information Gateway'); | |
| console.warn(' Presenter v0.0.1 '); | |
| console.warn('=============================='); | |
| console.warn(); | |
| /** | |
| * Load libraries | |
| */ | |
| console.warn('- Loading libraries'); | |
| console.warn(' `- Filesystem'); | |
| var fs = require('fs'); | |
| console.warn(' `- HTTP'); | |
| var http = require('http'); | |
| console.warn(' `- URL'); | |
| var url = require('url'); | |
| /** | |
| * Load config | |
| */ | |
| var config = JSON.parse(fs.readFileSync('config.json')); | |
| // Create mongo object | |
| require('assert').ok(config.db.database, 'Please define a database to use.'); | |
| if (config.db.poolSize === 'undefined') { config.db.poolSize = 1; } | |
| var mongo = new (require('mongo-pool').MongoPool)({db: config.db.database, poolSize: config.db.poolSize}); | |
| /** | |
| * Load reports | |
| */ | |
| var reports = {}; | |
| reports.events = { | |
| collection: 'events', | |
| fields: ['imei', 'time_sent', 'time_recv', 'latitude', 'longitude'], | |
| query: { | |
| 'imei': '<redacted>' | |
| } | |
| }; | |
| console.warn('- Starting HTTP server on '+config.net.bind+':'+config.net.port); | |
| var server = http.createServer(function(request, response) { | |
| var segments = url.parse(request.url).pathname.substr(1).split(/\//); | |
| response.on('finish', function() { console.log(JSON.stringify({time: new Date(), url: request.url, status: [response.statusCode, response.statusText]})); }); | |
| response.setHeader('Content-Type', 'application/json'); | |
| if (request.method != 'GET' && request.method != 'POST') { | |
| response.statusCode = 406; | |
| response.statusText = 'Invalid method'; | |
| response.end(JSON.stringify({error: 'Invalid method'})); | |
| return; | |
| } | |
| if (segments.length != 1 || !segments[0].match(/^[0-9a-z\-]+$/)) { | |
| response.statusCode = 406; | |
| response.statusText = 'Invalid URI'; | |
| response.end(JSON.stringify({error: 'Invalid URI'})); | |
| return; | |
| } | |
| var report_key = segments[0]; | |
| if (!(report_key in reports)) { | |
| response.statusCode = 404; | |
| response.statusText = 'Unknown Report'; | |
| response.end(JSON.stringify({error: 'Unknown Report'})); | |
| return; | |
| } | |
| report = reports[report_key]; | |
| if (request.method == 'GET') { | |
| response.statusCode = 200; | |
| response.statusText = 'OK'; | |
| response.end(JSON.stringify(report.fields)); | |
| return; | |
| } | |
| if (request.method == 'POST') { | |
| var data = ''; | |
| request.on('data', function(d) { | |
| data += d; | |
| }); | |
| request.on('end', function() { | |
| try { | |
| obj = data.length ? JSON.parse(data) : {}; | |
| } catch (e) { | |
| console.warn('[!] Error parsing JSON!'); | |
| response.statusCode = 406; | |
| response.statusText = 'Malformed JSON'; | |
| response.end(); | |
| return; | |
| } | |
| mongo.getClient(function(client) { | |
| if (!client) { | |
| console.warn('[!] Error getting MongoDB client'); | |
| response.statusCode = 500; | |
| response.statusText = 'Internal Server Error'; | |
| response.end(JSON.stringify({error: 'Database error'})); | |
| return; | |
| } | |
| mongo.getCollection(report.collection, function(err, collection) { | |
| if (err) { | |
| console.warn('[!] Error getting collection: '+err.message); | |
| response.statusCode = 500; | |
| response.statusText = 'Internal Server Error'; | |
| response.end(JSON.stringify({error: 'Database error'})); | |
| mongo.release(client); | |
| return; | |
| } | |
| // (report.query) is fine | |
| // (report.query, report.fields) results in an error (field list is mistaken for option object?) | |
| // (report.query, report.fields, {}) results in... odd things :( | |
| var cursor = collection.find(report.query, report.fields, {}); | |
| if (!cursor) { | |
| console.warn('[!] Error getting cursor'); | |
| response.statusCode = 500; | |
| response.statusText = 'Internal Server Error'; | |
| response.end(JSON.stringify({error: 'Database error'})); | |
| mongo.release(client); | |
| return; | |
| } | |
| cursor.limit(5); | |
| cursor.toArray(function(err, results) { | |
| if (err) { | |
| console.warn('[!] Error iterating cursor: '+err.message); | |
| response.statusCode = 500; | |
| response.statusText = 'Internal Server Error'; | |
| response.end(JSON.stringify({error: 'Database error'})); | |
| mongo.release(client); | |
| return; | |
| } | |
| response.statusCode = 200; | |
| response.statusText = 'OK'; | |
| response.end(JSON.stringify({headers: report.fields, data: results})); | |
| mongo.release(client); | |
| return; | |
| }); | |
| }); | |
| }); | |
| }); | |
| return; | |
| } | |
| }); | |
| server.listen(config.net.port, config.net.bind); | |
| console.warn(' `- HTTP server ready'); | |
| console.warn(); | |
| console.warn('==================='); | |
| console.warn(' Server is running '); | |
| console.warn('==================='); | |
| console.warn(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment