switch from mongodb-backed data to filesystem-backed
This commit is contained in:
parent
de45c9b411
commit
24167d5b45
6
data/comp-b51422fbc0480e544784b4c2b119f666ea8c1e2b.json
Normal file
6
data/comp-b51422fbc0480e544784b4c2b119f666ea8c1e2b.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"created": 1429314628678,
|
||||||
|
"a": "a",
|
||||||
|
"b": "n",
|
||||||
|
"_id": "b51422fbc0480e544784b4c2b119f666ea8c1e2b"
|
||||||
|
}
|
@ -4,6 +4,7 @@
|
|||||||
"main": "server/app.js",
|
"main": "server/app.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "~1.5.0",
|
"body-parser": "~1.5.0",
|
||||||
|
"chance": "~0.7.3",
|
||||||
"composable-middleware": "^0.3.0",
|
"composable-middleware": "^0.3.0",
|
||||||
"compression": "~1.0.1",
|
"compression": "~1.0.1",
|
||||||
"connect-mongo": "^0.4.1",
|
"connect-mongo": "^0.4.1",
|
||||||
@ -13,6 +14,7 @@
|
|||||||
"express-jwt": "^0.1.3",
|
"express-jwt": "^0.1.3",
|
||||||
"express-session": "~1.0.2",
|
"express-session": "~1.0.2",
|
||||||
"jade": "~1.2.0",
|
"jade": "~1.2.0",
|
||||||
|
"jsonfile": "~2.0.0",
|
||||||
"jsonwebtoken": "^0.3.0",
|
"jsonwebtoken": "^0.3.0",
|
||||||
"lex": "^1.7.8",
|
"lex": "^1.7.8",
|
||||||
"lodash": "~2.4.1",
|
"lodash": "~2.4.1",
|
||||||
|
@ -4,54 +4,84 @@
|
|||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var Comparison = require('./comparison.model');
|
var Comparison = require('./comparison.model');
|
||||||
var wdiff = require('../../components/wdiff');
|
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
|
//return the comparison given an id, if it exsits
|
||||||
exports.showComparison = function showComparison(req, res) {
|
exports.showComparison = function showComparison(req, res) {
|
||||||
Comparison.findById(req.params.id).exec(function (err, 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); }
|
if(err) { return handleError(res, err); }
|
||||||
if(!comparison) { return res.send(404); }
|
|
||||||
|
//and return
|
||||||
return res.json(comparison);
|
return res.json(comparison);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//return a markdown wdiff for the comparison given an id, if it exsits
|
//return a markdown wdiff for the comparison given an id, if it exsits
|
||||||
exports.wdiffMarkdownComparison = function wdiffMarkdownComparison(req, res) {
|
exports.wdiffMarkdownComparison = function wdiffMarkdownComparison(req, res) {
|
||||||
Comparison.findById(req.params.id).exec(function (err, comparison) {
|
//generate a filename
|
||||||
if(err) { return handleError(res, err); }
|
var filename = fnComparison(req.params.id);
|
||||||
if(!comparison) { return res.send(404); }
|
|
||||||
|
|
||||||
|
//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); }
|
||||||
|
|
||||||
|
//now perform a wdiff on the result
|
||||||
wdiff(comparison.a,comparison.b, true, function(err, result) {
|
wdiff(comparison.a,comparison.b, true, function(err, result) {
|
||||||
if (err)
|
if (err)
|
||||||
return handleError(res, err);
|
return handleError(res, err);
|
||||||
|
|
||||||
_.merge(result, comparison._doc)
|
_.merge(result, comparison)
|
||||||
return res.json(result);
|
return res.json(result);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new comparison
|
// Creates a new comparison
|
||||||
exports.create = function(req, res) {
|
exports.create = function create(req, res) {
|
||||||
//we do not allow the api client to change the id willy-nilly!
|
var a = req.body.a;
|
||||||
if (req.body._id) { delete req.body._id; }
|
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
|
//and write it to the filesystem
|
||||||
Comparison.create(comparison, function(err, comparison) {
|
jf.writeFile(filename, comparison, function(err) {
|
||||||
if(err) { return handleError(res, err); }
|
if(err) { return handleError(res, err); }
|
||||||
|
|
||||||
//save the document and return
|
//if successful, return the comparison object
|
||||||
comparison.save(function (err, comparison) {
|
|
||||||
if(err) { return handleError(res, err); }
|
|
||||||
return res.json(201, comparison);
|
return res.json(201, comparison);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function handleError(res, err) {
|
function handleError(res, err) {
|
||||||
|
console.log(err);
|
||||||
return res.send(500, err);
|
return res.send(500, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// returns a filename for the given comparison
|
||||||
|
function fnComparison (id) {
|
||||||
|
return "./data/" + "comp-" + id + ".json";
|
||||||
|
}
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var mongoose = require('mongoose'),
|
// Load Chance
|
||||||
Schema = mongoose.Schema;
|
var Chance = require('chance');
|
||||||
|
|
||||||
var ComparisonSchema = new Schema({
|
// Instantiate Chance so it can be used
|
||||||
created: {type: Date, default: Date.now},
|
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;
|
||||||
|
@ -8,14 +8,8 @@
|
|||||||
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
||||||
|
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var mongoose = require('mongoose');
|
|
||||||
var config = require('./config/environment');
|
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
|
// Setup server
|
||||||
var app = express();
|
var app = express();
|
||||||
|
@ -15,8 +15,6 @@ var errorHandler = require('errorhandler');
|
|||||||
var path = require('path');
|
var path = require('path');
|
||||||
var config = require('./environment');
|
var config = require('./environment');
|
||||||
var session = require('express-session');
|
var session = require('express-session');
|
||||||
var mongoStore = require('connect-mongo')(session);
|
|
||||||
var mongoose = require('mongoose');
|
|
||||||
|
|
||||||
module.exports = function(app) {
|
module.exports = function(app) {
|
||||||
var env = app.get('env');
|
var env = app.get('env');
|
||||||
@ -28,16 +26,7 @@ module.exports = function(app) {
|
|||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(methodOverride());
|
app.use(methodOverride());
|
||||||
app.use(cookieParser());
|
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) {
|
if ('production' === env) {
|
||||||
app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
|
app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
|
||||||
|
@ -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';
|
|
Loading…
Reference in New Issue
Block a user