From 44f7ab3f3311f85dca56a99f2bec4a36e0c5dddc Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Fri, 16 Dec 2011 07:57:09 -0500 Subject: [PATCH] Added the ability to retrieve raw documents Closes #10 --- lib/document_handler.js | 16 ++++++++++++++++ server.js | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/lib/document_handler.js b/lib/document_handler.js index 07e824f..f30defa 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -29,6 +29,22 @@ DocumentHandler.prototype.handleGet = function(key, response, skipExpire) { }, skipExpire); }; +// Handle retrieving the raw version of a document +DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) { + this.store.get(key, function(ret) { + if (ret) { + winston.verbose('retrieved raw document', { key: key }); + response.writeHead(200, { 'content-type': 'text/plain' }); + response.end(ret); + } + else { + winston.warn('raw document not found', { key: key }); + response.writeHead(404, { 'content-type': 'application/json' }); + response.end(JSON.stringify({ message: 'document not found' })); + } + }, skipExpire); +}; + // Handle adding a new Document DocumentHandler.prototype.handlePost = function(request, response) { var _this = this; diff --git a/server.js b/server.js index 330c9f3..54f85d5 100644 --- a/server.js +++ b/server.js @@ -83,6 +83,12 @@ var documentHandler = new DocumentHandler({ connect.createServer( // First look for api calls connect.router(function(app) { + // get raw documents - support getting with extension + app.get('/raw/:id', function(request, response, next) { + var skipExpire = !!config.documents[request.params.id]; + var key = request.params.id.split('.')[0]; + return documentHandler.handleRawGet(key, response, skipExpire); + }); // add documents app.post('/documents', function(request, response, next) { return documentHandler.handlePost(request, response);