Clean up some code and move document stores into subfolder
This commit is contained in:
parent
483fce891d
commit
f37d1ad401
10 changed files with 92 additions and 44 deletions
|
@ -54,13 +54,15 @@ DocumentHandler.prototype.handlePost = function(request, response) {
|
|||
request.on('data', function(data) {
|
||||
if (!buffer) {
|
||||
response.writeHead(200, { 'content-type': 'application/json' });
|
||||
}
|
||||
}
|
||||
buffer += data.toString();
|
||||
if (_this.maxLength && buffer.length > _this.maxLength) {
|
||||
cancelled = true;
|
||||
winston.warn('document >maxLength', { maxLength: _this.maxLength });
|
||||
response.writeHead(400, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify({ message: 'Document exceeds maximum length.' }));
|
||||
response.end(
|
||||
JSON.stringify({ message: 'Document exceeds maximum length.' })
|
||||
);
|
||||
}
|
||||
});
|
||||
request.on('end', function(end) {
|
||||
|
@ -96,7 +98,7 @@ DocumentHandler.prototype.chooseKey = function(callback) {
|
|||
} else {
|
||||
callback(key);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
DocumentHandler.prototype.acceptableKey = function() {
|
||||
|
|
|
@ -19,19 +19,21 @@ FileDocumentStore.md5 = function(str) {
|
|||
return md5sum.digest('hex');
|
||||
};
|
||||
|
||||
// Save data in a file, key as md5 - since we don't know what we could be passed here
|
||||
// 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, skipExpire) {
|
||||
try {
|
||||
var _this = this;
|
||||
fs.mkdir(this.basePath, '700', function() {
|
||||
fs.writeFile(_this.basePath + '/' + _this.md5(key), data, 'utf8', function(err) {
|
||||
var fn = _this.basePath + '/' + _this.md5(key);
|
||||
fs.writeFile(fn, data, 'utf8', function(err) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
}
|
||||
else {
|
||||
callback(true);
|
||||
if (_this.expire && !skipExpire) {
|
||||
winston.warn('file store cannot set expirations on keys');
|
||||
winston.warn('file store cannot set expirations on keys');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -44,14 +46,15 @@ FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
|
|||
// Get data from a file from key
|
||||
FileDocumentStore.prototype.get = function(key, callback, skipExpire) {
|
||||
var _this = this;
|
||||
fs.readFile(this.basePath + '/' + _this..md5(key), 'utf8', function(err, data) {
|
||||
var fn = this.basePath + '/' + this.md5(key);
|
||||
fs.readFile(fn, 'utf8', function(err, data) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
}
|
||||
else {
|
||||
callback(data);
|
||||
if (_this.expire && !skipExpire) {
|
||||
winston.warn('file store cannot set expirations on keys');
|
||||
winston.warn('file store cannot set expirations on keys');
|
||||
}
|
||||
}
|
||||
});
|
|
@ -24,7 +24,8 @@ MemcachedDocumentStore.connect = function(options) {
|
|||
};
|
||||
|
||||
// Save file in a key
|
||||
MemcachedDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
|
||||
MemcachedDocumentStore.prototype.set =
|
||||
function(key, data, callback, skipExpire) {
|
||||
MemcachedDocumentStore.client.set(key, data, function(err, reply) {
|
||||
err ? callback(false) : callback(true);
|
||||
}, skipExpire ? 0 : this.expire);
|
||||
|
@ -36,7 +37,7 @@ MemcachedDocumentStore.prototype.get = function(key, callback, skipExpire) {
|
|||
MemcachedDocumentStore.client.get(key, function(err, reply) {
|
||||
callback(err ? false : reply);
|
||||
if (_this.expire && !skipExpire) {
|
||||
winston.warn('memcache store does not currently push forward expirations on GET');
|
||||
winston.warn('store does not currently push forward expirations on GET');
|
||||
}
|
||||
});
|
||||
};
|
|
@ -23,7 +23,10 @@ RedisDocumentStore.connect = function(options) {
|
|||
RedisDocumentStore.client = redis.createClient(port, host);
|
||||
RedisDocumentStore.client.select(index, function(err, reply) {
|
||||
if (err) {
|
||||
winston.error('error connecting to redis index ' + index, { error: err.message });
|
||||
winston.error(
|
||||
'error connecting to redis index ' + index,
|
||||
{ error: err.message }
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
else {
|
||||
|
@ -67,7 +70,7 @@ RedisDocumentStore.prototype.get = function(key, callback, skipExpire) {
|
|||
_this.setExpiration(key);
|
||||
}
|
||||
callback(err ? false : reply);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = RedisDocumentStore;
|
|
@ -8,8 +8,10 @@ var RandomKeyGenerator = function(options) {
|
|||
// Generate a random key
|
||||
RandomKeyGenerator.prototype.createKey = function(keyLength) {
|
||||
var text = '';
|
||||
var index;
|
||||
for (var i = 0; i < keyLength; i++) {
|
||||
text += this.keyspace.charAt(Math.floor(Math.random() * this.keyspace.length));
|
||||
index = Math.floor(Math.random() * this.keyspace.length);
|
||||
text += this.keyspace.charAt(index);
|
||||
}
|
||||
return text;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue