From fec02cfeadd075d0c3a22de3bf28b2d342ec7cb9 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Fri, 18 Nov 2011 17:54:57 -0500 Subject: [PATCH] Move storage mechanism to new object --- lib/document_handler.js | 34 +++++----------------------------- lib/file_document_store.js | 36 ++++++++++++++++++++++++++++++++++++ server.js | 10 ++++++++-- 3 files changed, 49 insertions(+), 31 deletions(-) create mode 100644 lib/file_document_store.js diff --git a/lib/document_handler.js b/lib/document_handler.js index 3e7a903..8de73ef 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -1,42 +1,17 @@ -var fs = require('fs'); // TODO won't be needed - var winston = require('winston'); -var hashlib = require('hashlib'); // For handling serving stored documents var DocumentHandler = function(options) { if (options) { - this.keyLength = options.keyLength || 20; + this.keyLength = options.keyLength || 10; + this.store = options.store; } }; -// Save a document -// TODO make data path configurable -// TODO move to a separate object -DocumentHandler.save = function(key, data, callback) { - fs.mkdir('data', '700', function() { - fs.writeFile('data/' + hashlib.md5(key), data, 'utf8', function() { - callback(true); // TODO handle errors - }); - }); -}; - -// Retrieve a document by key -DocumentHandler.get = function(key, callback) { - fs.readFile('data/' + hashlib.md5(key), 'utf8', function(err, data) { - if (err) { - callback(false); - } - else { - callback(data); - } - }); -}; - // Handle retrieving a document DocumentHandler.prototype.handleGet = function(key, response) { - DocumentHandler.get(key, function(ret) { + this.store.get(key, function(ret) { if (ret) { winston.verbose('retrieved document', { key: key }); response.writeHead(200, { 'content-type': 'application/json' }); @@ -52,6 +27,7 @@ DocumentHandler.prototype.handleGet = function(key, response) { // Handle adding a new Document DocumentHandler.prototype.handlePost = function(request, response) { + var _this = this; var key = this.randomKey(); var buffer = ''; request.on('data', function(data) { @@ -61,7 +37,7 @@ DocumentHandler.prototype.handlePost = function(request, response) { buffer += data.toString(); }); request.on('end', function(end) { - DocumentHandler.save(key, buffer, function(res) { + _this.store.set(key, buffer, function(res) { if (res) { winston.verbose('added document', { key: key }); response.end(JSON.stringify({ key: key })); diff --git a/lib/file_document_store.js b/lib/file_document_store.js new file mode 100644 index 0000000..3fe0326 --- /dev/null +++ b/lib/file_document_store.js @@ -0,0 +1,36 @@ +var fs = require('fs'); + +var winston = require('winston'); +var hashlib = require('hashlib'); + +// For storing in files +// TODO make data path configurable +// TODO make store type configurable + +var FileDocumentStore = function(path) { + this.basePath = path; +}; + +// Save data in a file, key as md5 - since we don't know what we could be passed here +FileDocumentStore.prototype.set = function(key, data, callback) { + var _this = this; + fs.mkdir(this.basePath, '700', function() { + fs.writeFile(_this.basePath + '/' + hashlib.md5(key), data, 'utf8', function() { + callback(true); // TODO handle errors + }); + }); +}; + +// Get data from a file from key +FileDocumentStore.prototype.get = function(key, callback) { + fs.readFile(this.basePath + '/' + hashlib.md5(key), 'utf8', function(err, data) { + if (err) { + callback(false); + } + else { + callback(data); + } + }); +}; + +module.exports = FileDocumentStore; diff --git a/server.js b/server.js index 5fb28bb..ed86a1a 100644 --- a/server.js +++ b/server.js @@ -6,6 +6,7 @@ var winston = require('winston'); var StaticHandler = require('./lib/static_handler'); var DocumentHandler = require('./lib/document_handler'); +var FileDocumentStore = require('./lib/file_document_store'); // Load the configuration and set some defaults var config = JSON.parse(fs.readFileSync('config.js', 'utf8')); @@ -36,14 +37,19 @@ http.createServer(function(request, response) { // Looking to add a new doc if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') { - handler = new DocumentHandler({ keyLength: config.keyLength }); + handler = new DocumentHandler({ + keyLength: config.keyLength, + store: new FileDocumentStore('./data') + }); return handler.handlePost(request, response); } // Looking up a doc var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/); if (request.method == 'GET' && match) { - handler = new DocumentHandler(); + handler = new DocumentHandler({ + store: new FileDocumentStore('./data') + }); return handler.handleGet(match[1], response); }