mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-22 04:31:21 +00:00
Move storage mechanism to new object
This commit is contained in:
parent
b173cdd144
commit
fec02cfead
3 changed files with 49 additions and 31 deletions
|
@ -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 }));
|
||||
|
|
36
lib/file_document_store.js
Normal file
36
lib/file_document_store.js
Normal file
|
@ -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;
|
10
server.js
10
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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue