From 24167d5b45f8aa723ad48f0aaa01eb771e014bd7 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 17 Apr 2015 19:53:16 -0400 Subject: [PATCH] switch from mongodb-backed data to filesystem-backed --- ...422fbc0480e544784b4c2b119f666ea8c1e2b.json | 6 ++ package.json | 2 + .../api/comparison/comparison.controller.js | 78 +++++++++++++------ server/api/comparison/comparison.model.js | 22 ++++-- server/app.js | 8 +- server/config/express.js | 13 +--- server/config/seed.js | 6 -- 7 files changed, 78 insertions(+), 57 deletions(-) create mode 100644 data/comp-b51422fbc0480e544784b4c2b119f666ea8c1e2b.json delete mode 100644 server/config/seed.js diff --git a/data/comp-b51422fbc0480e544784b4c2b119f666ea8c1e2b.json b/data/comp-b51422fbc0480e544784b4c2b119f666ea8c1e2b.json new file mode 100644 index 0000000..ce53ae2 --- /dev/null +++ b/data/comp-b51422fbc0480e544784b4c2b119f666ea8c1e2b.json @@ -0,0 +1,6 @@ +{ + "created": 1429314628678, + "a": "a", + "b": "n", + "_id": "b51422fbc0480e544784b4c2b119f666ea8c1e2b" +} diff --git a/package.json b/package.json index 4d23f53..45b988a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/api/comparison/comparison.controller.js b/server/api/comparison/comparison.controller.js index f1867dc..c85e6fd 100644 --- a/server/api/comparison/comparison.controller.js +++ b/server/api/comparison/comparison.controller.js @@ -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"; +} diff --git a/server/api/comparison/comparison.model.js b/server/api/comparison/comparison.model.js index 29114ba..51fb2d6 100644 --- a/server/api/comparison/comparison.model.js +++ b/server/api/comparison/comparison.model.js @@ -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; diff --git a/server/app.js b/server/app.js index 82acdba..c0a3094 100644 --- a/server/app.js +++ b/server/app.js @@ -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; \ No newline at end of file +exports = module.exports = app; diff --git a/server/config/express.js b/server/config/express.js index 30edb8c..0f6131e 100644 --- a/server/config/express.js +++ b/server/config/express.js @@ -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'))); diff --git a/server/config/seed.js b/server/config/seed.js deleted file mode 100644 index 44beecd..0000000 --- a/server/config/seed.js +++ /dev/null @@ -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';