mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-22 04:31:21 +00:00
Added FS backed
This commit is contained in:
parent
93435715f2
commit
7709e12500
2 changed files with 51 additions and 17 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,3 +2,4 @@ npm-debug.log
|
||||||
node_modules
|
node_modules
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
|
data
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
|
var fs = require('fs'); // TODO won't be needed
|
||||||
|
|
||||||
var winston = require('winston');
|
var winston = require('winston');
|
||||||
|
var hashlib = require('hashlib');
|
||||||
|
|
||||||
// For handling serving stored documents
|
// For handling serving stored documents
|
||||||
|
|
||||||
|
@ -8,36 +11,66 @@ var DocumentHandler = function(options) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO implement with FS backend
|
// Save a document
|
||||||
DocumentHandler.documents = {};
|
// 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
|
// Handle retrieving a document
|
||||||
DocumentHandler.prototype.handleGet = function(key, response) {
|
DocumentHandler.prototype.handleGet = function(key, response) {
|
||||||
if (DocumentHandler.documents[key]) {
|
DocumentHandler.get(key, function(ret) {
|
||||||
|
if (ret) {
|
||||||
winston.verbose('retrieved document', { key: key });
|
winston.verbose('retrieved document', { key: key });
|
||||||
response.writeHead(200, { 'content-type': 'application/json' });
|
response.writeHead(200, { 'content-type': 'application/json' });
|
||||||
response.end(JSON.stringify({ data: DocumentHandler.documents[key], key: key }));
|
response.end(JSON.stringify({ data: ret, key: key }));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
winston.warn('document not found', { key: key });
|
winston.warn('document not found', { key: key });
|
||||||
response.writeHead(404, { 'content-type': 'application/json' });
|
response.writeHead(404, { 'content-type': 'application/json' });
|
||||||
response.end(JSON.stringify({ message: 'document not found' }));
|
response.end(JSON.stringify({ message: 'document not found' }));
|
||||||
}
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle adding a new Document
|
// Handle adding a new Document
|
||||||
DocumentHandler.prototype.handlePost = function(request, response) {
|
DocumentHandler.prototype.handlePost = function(request, response) {
|
||||||
var key = this.randomKey();
|
var key = this.randomKey();
|
||||||
|
var buffer = '';
|
||||||
request.on('data', function(data) {
|
request.on('data', function(data) {
|
||||||
if (!DocumentHandler.documents[key]) {
|
if (!buffer) {
|
||||||
response.writeHead(200, { 'content-type': 'application/json' });
|
response.writeHead(200, { 'content-type': 'application/json' });
|
||||||
DocumentHandler.documents[key] = '';
|
|
||||||
}
|
}
|
||||||
DocumentHandler.documents[key] += data.toString();
|
buffer += data.toString();
|
||||||
});
|
});
|
||||||
request.on('end', function(end) {
|
request.on('end', function(end) {
|
||||||
|
DocumentHandler.save(key, buffer, function(res) {
|
||||||
|
if (res) {
|
||||||
winston.verbose('added document', { key: key });
|
winston.verbose('added document', { key: key });
|
||||||
response.end(JSON.stringify({ key: key }));
|
response.end(JSON.stringify({ key: key }));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
winston.verbose('error adding document');
|
||||||
|
response.end(JSON.stringify({ message: 'error adding document' }));
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
request.on('error', function(error) {
|
request.on('error', function(error) {
|
||||||
winston.error('connection error: ' + error.message);
|
winston.error('connection error: ' + error.message);
|
||||||
|
|
Loading…
Reference in a new issue