|
|
|
@ -16,33 +16,55 @@ var DocumentHandler = function(options) { |
|
|
|
|
DocumentHandler.defaultKeyLength = 10; |
|
|
|
|
|
|
|
|
|
// Handle retrieving a document
|
|
|
|
|
DocumentHandler.prototype.handleGet = function(key, response, skipExpire) { |
|
|
|
|
DocumentHandler.prototype.handleGet = function(request, response, config) { |
|
|
|
|
const key = request.params.id.split('.')[0]; |
|
|
|
|
const skipExpire = !!config.documents[key]; |
|
|
|
|
|
|
|
|
|
this.store.get(key, function(ret) { |
|
|
|
|
if (ret) { |
|
|
|
|
winston.verbose('retrieved document', { key: key }); |
|
|
|
|
response.writeHead(200, { 'content-type': 'application/json' }); |
|
|
|
|
response.end(JSON.stringify({ data: ret, key: key })); |
|
|
|
|
if (request.method === 'HEAD') { |
|
|
|
|
response.end(); |
|
|
|
|
} else { |
|
|
|
|
response.end(JSON.stringify({ data: ret, key: key })); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
winston.warn('document not found', { key: key }); |
|
|
|
|
response.writeHead(404, { 'content-type': 'application/json' }); |
|
|
|
|
response.end(JSON.stringify({ message: 'Document not found.' })); |
|
|
|
|
if (request.method === 'HEAD') { |
|
|
|
|
response.end(); |
|
|
|
|
} else { |
|
|
|
|
response.end(JSON.stringify({ message: 'Document not found.' })); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, skipExpire); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Handle retrieving the raw version of a document
|
|
|
|
|
DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) { |
|
|
|
|
DocumentHandler.prototype.handleRawGet = function(request, response, config) { |
|
|
|
|
const key = request.params.id.split('.')[0]; |
|
|
|
|
const skipExpire = !!config.documents[key]; |
|
|
|
|
|
|
|
|
|
this.store.get(key, function(ret) { |
|
|
|
|
if (ret) { |
|
|
|
|
winston.verbose('retrieved raw document', { key: key }); |
|
|
|
|
response.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' }); |
|
|
|
|
response.end(ret); |
|
|
|
|
if (request.method === 'HEAD') { |
|
|
|
|
response.end(); |
|
|
|
|
} else { |
|
|
|
|
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.' })); |
|
|
|
|
if (request.method === 'HEAD') { |
|
|
|
|
response.end(); |
|
|
|
|
} else { |
|
|
|
|
response.end(JSON.stringify({ message: 'Document not found.' })); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, skipExpire); |
|
|
|
|
}; |
|
|
|
|