|
|
|
@ -3,6 +3,8 @@ var fs = require('fs'); |
|
|
|
|
|
|
|
|
|
var winston = require('winston'); |
|
|
|
|
|
|
|
|
|
var DocumentHandler = require('./document_handler'); |
|
|
|
|
|
|
|
|
|
// For serving static assets
|
|
|
|
|
|
|
|
|
|
var StaticHandler = function(path, cacheAssets) { |
|
|
|
@ -33,17 +35,38 @@ StaticHandler.contentTypeFor = function(ext) { |
|
|
|
|
|
|
|
|
|
// Handle a request, and serve back the asset if it exists
|
|
|
|
|
StaticHandler.prototype.handle = function(incPath, response) { |
|
|
|
|
// Go to index if not found or /
|
|
|
|
|
if (!this.availablePaths[incPath]) incPath = this.defaultPath; |
|
|
|
|
var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); |
|
|
|
|
// If this is a potential key, show the index - otherwise
|
|
|
|
|
// bust out a 404
|
|
|
|
|
if (!this.availablePaths[incPath]) { |
|
|
|
|
if (incPath === '/' || DocumentHandler.potentialKey(incPath.substring(1))) { |
|
|
|
|
incPath = this.defaultPath; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
winston.warn('failed to find static asset', { path: incPath }); |
|
|
|
|
response.writeHead(404, { 'content-type': 'application/json' }); |
|
|
|
|
response.end(JSON.stringify({ message: 'no such file' })); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// And then stream the file back - either from the cache or from source
|
|
|
|
|
var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); |
|
|
|
|
var cached = this.cacheAssets && this.isCached(filePath); |
|
|
|
|
var method = cached ? this.serveCached : this.retrieve; |
|
|
|
|
// Run!
|
|
|
|
|
// Go get'er
|
|
|
|
|
var _this = this; |
|
|
|
|
var method = cached ? this.serveCached : this.retrieve; |
|
|
|
|
method(filePath, function(error, content) { |
|
|
|
|
// detect errors
|
|
|
|
|
if (error) { |
|
|
|
|
winston.error('unable to read file', { path: filePath, error: error }); |
|
|
|
|
response.writeHead(500, { 'content-type': 'application/json' }); |
|
|
|
|
response.end(JSON.stringify({ message: 'IO: Unable to read file' })); |
|
|
|
|
// If it was cached, bust the cache
|
|
|
|
|
if (cached) { |
|
|
|
|
StaticHandler.cache[filePath] = null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// Get the content
|
|
|
|
|
if (content) { |
|
|
|
|
else { |
|
|
|
|
var contentType = StaticHandler.contentTypeFor(path.extname(filePath)); |
|
|
|
|
response.writeHead(200, { 'content-type': contentType }); |
|
|
|
|
response.end(content, 'utf-8'); |
|
|
|
@ -52,16 +75,6 @@ StaticHandler.prototype.handle = function(incPath, response) { |
|
|
|
|
StaticHandler.cache[filePath] = content; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// Or break down and cry
|
|
|
|
|
else { |
|
|
|
|
winston.error('unable to read file', { path: filePath, error: error }); |
|
|
|
|
response.writeHead(500, { 'content-type': 'application/json' }); |
|
|
|
|
response.end(JSON.stringify({ message: 'IO: Unable to read file' })); |
|
|
|
|
// If it was cached, bust the cache
|
|
|
|
|
if (cached) { |
|
|
|
|
StaticHandler.cache[filePath] = null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
});
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|