switch from mongodb-backed data to filesystem-backed

This commit is contained in:
Adam Brown 2015-04-17 19:53:16 -04:00
parent de45c9b411
commit 24167d5b45
7 changed files with 78 additions and 57 deletions

View File

@ -0,0 +1,6 @@
{
"created": 1429314628678,
"a": "a",
"b": "n",
"_id": "b51422fbc0480e544784b4c2b119f666ea8c1e2b"
}

View File

@ -4,6 +4,7 @@
"main": "server/app.js",
"dependencies": {
"body-parser": "~1.5.0",
"chance": "~0.7.3",
"composable-middleware": "^0.3.0",
"compression": "~1.0.1",
"connect-mongo": "^0.4.1",
@ -13,6 +14,7 @@
"express-jwt": "^0.1.3",
"express-session": "~1.0.2",
"jade": "~1.2.0",
"jsonfile": "~2.0.0",
"jsonwebtoken": "^0.3.0",
"lex": "^1.7.8",
"lodash": "~2.4.1",

View File

@ -4,54 +4,84 @@
var _ = require('lodash');
var Comparison = require('./comparison.model');
var wdiff = require('../../components/wdiff');
var mongoose = require('mongoose');
var jf = require('jsonfile');
var fs = require('fs');
//return the comparison given an id, if it exsits
exports.showComparison = function showComparison(req, res) {
Comparison.findById(req.params.id).exec(function (err, comparison) {
if(err) { return handleError(res, err); }
if(!comparison) { return res.send(404); }
return res.json(comparison);
//generate a filename
var filename = fnComparison(req.params.id);
//check if that file exists
fs.exists(filename, function (exists) {
//if the file does not exist, return a 404
if (!exists) return res.send(404);
//otherwise, read the file as JSON
jf.readFile(filename, function(err, comparison) {
if(err) { return handleError(res, err); }
//and return
return res.json(comparison);
});
});
}
//return a markdown wdiff for the comparison given an id, if it exsits
exports.wdiffMarkdownComparison = function wdiffMarkdownComparison(req, res) {
Comparison.findById(req.params.id).exec(function (err, comparison) {
if(err) { return handleError(res, err); }
if(!comparison) { return res.send(404); }
//generate a filename
var filename = fnComparison(req.params.id);
wdiff(comparison.a,comparison.b, true, function(err, result) {
if (err)
return handleError(res, err);
//check if that file exists
fs.exists(filename, function (exists) {
//if the file does not exist, return a 404
if (!exists) return res.send(404);
_.merge(result, comparison._doc)
return res.json(result);
//otherwise, read the file as JSON
jf.readFile(filename, function(err, comparison) {
if(err) { return handleError(res, err); }
//now perform a wdiff on the result
wdiff(comparison.a,comparison.b, true, function(err, result) {
if (err)
return handleError(res, err);
_.merge(result, comparison)
return res.json(result);
});
});
});
}
// Creates a new comparison
exports.create = function(req, res) {
//we do not allow the api client to change the id willy-nilly!
if (req.body._id) { delete req.body._id; }
exports.create = function create(req, res) {
var a = req.body.a;
var b = req.body.b;
var comparison = req.body;
//create the comparison
var comparison = new Comparison(a,b);
//look up its filename
var filename = fnComparison(comparison._id);
//and add to the db
Comparison.create(comparison, function(err, comparison) {
//and write it to the filesystem
jf.writeFile(filename, comparison, function(err) {
if(err) { return handleError(res, err); }
//save the document and return
comparison.save(function (err, comparison) {
if(err) { return handleError(res, err); }
return res.json(201, comparison);
});
//if successful, return the comparison object
return res.json(201, comparison);
});
};
function handleError(res, err) {
console.log(err);
return res.send(500, err);
}
// returns a filename for the given comparison
function fnComparison (id) {
return "./data/" + "comp-" + id + ".json";
}

View File

@ -1,13 +1,19 @@
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
// Load Chance
var Chance = require('chance');
var ComparisonSchema = new Schema({
created: {type: Date, default: Date.now},
// Instantiate Chance so it can be used
var chance = new Chance();
a: String,
b: String,
});
module.exports = mongoose.model('Comparison', ComparisonSchema);
var Comparison = function Comparison(a,b) {
return {
created: Date.now(),
a: a,
b: b,
_id: chance.hash()
};
};
module.exports = Comparison;

View File

@ -8,14 +8,8 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var express = require('express');
var mongoose = require('mongoose');
var config = require('./config/environment');
// Connect to database
mongoose.connect(config.mongo.uri, config.mongo.options);
// Populate DB with sample data
if(config.seedDB) { require('./config/seed'); }
// Setup server
var app = express();
@ -29,4 +23,4 @@ server.listen(config.port, config.ip, function () {
});
// Expose app
exports = module.exports = app;
exports = module.exports = app;

View File

@ -15,8 +15,6 @@ var errorHandler = require('errorhandler');
var path = require('path');
var config = require('./environment');
var session = require('express-session');
var mongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');
module.exports = function(app) {
var env = app.get('env');
@ -28,17 +26,8 @@ module.exports = function(app) {
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser());
app.use(passport.initialize());
// Persist sessions with mongoStore
// We need to enable sessions for passport twitter because its an oauth 1.0 strategy
app.use(session({
secret: config.secrets.session,
resave: true,
saveUninitialized: true,
store: new mongoStore({ mongoose_connection: mongoose.connection })
}));
if ('production' === env) {
app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
app.use(express.static(path.join(config.root, 'public')));

View File

@ -1,6 +0,0 @@
/**
* Populate DB with sample data on server start
* to disable, edit config/environment/index.js, and set `seedDB: false`
*/
'use strict';