|
|
@ -35,40 +35,50 @@ StaticHandler.prototype.handle = function(incPath, response) { |
|
|
|
// Go to index if not found or /
|
|
|
|
// Go to index if not found or /
|
|
|
|
if (!this.availablePaths[incPath]) incPath = this.defaultPath; |
|
|
|
if (!this.availablePaths[incPath]) incPath = this.defaultPath; |
|
|
|
var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); |
|
|
|
var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); |
|
|
|
// And then stream the file back
|
|
|
|
// And then stream the file back - either from the cache or from source
|
|
|
|
if (StaticHandler.cache[filePath]) { |
|
|
|
var cached = this.isCached(filePath); |
|
|
|
this.serveCached(filePath, response); |
|
|
|
var method = cached ? this.serveCached : this.retrieve; |
|
|
|
} |
|
|
|
// Run!
|
|
|
|
else { |
|
|
|
method(filePath, function(error, content) { |
|
|
|
this.retrieve(filePath, response); |
|
|
|
// Get the content
|
|
|
|
} |
|
|
|
if (content) { |
|
|
|
|
|
|
|
var contentType = StaticHandler.contentTypeFor(path.extname(filePath)); |
|
|
|
|
|
|
|
response.writeHead(200, { 'content-type': contentType }); |
|
|
|
|
|
|
|
response.end(content, 'utf-8'); |
|
|
|
|
|
|
|
// Stick it in the cache if its not in there
|
|
|
|
|
|
|
|
if (!cached) { |
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
});
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Retrieve from the file
|
|
|
|
// Retrieve from the file
|
|
|
|
StaticHandler.prototype.retrieve = function(filePath, response) { |
|
|
|
StaticHandler.prototype.retrieve = function(filePath, callback) { |
|
|
|
var _this = this; |
|
|
|
var _this = this; |
|
|
|
fs.readFile(filePath, function(error, content) { |
|
|
|
fs.readFile(filePath, function(error, content) { |
|
|
|
if (error) { |
|
|
|
callback(error, content); |
|
|
|
winston.error('unable to read file', { path: filePath, error: error.message }); |
|
|
|
|
|
|
|
response.writeHead(500, { 'content-type': 'application/json' }); |
|
|
|
|
|
|
|
response.end(JSON.stringify({ message: 'IO: Unable to read file' })); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
var contentType = StaticHandler.contentTypeFor(path.extname(filePath)); |
|
|
|
|
|
|
|
response.writeHead(200, { 'content-type': contentType }); |
|
|
|
|
|
|
|
response.end(content, 'utf-8'); |
|
|
|
|
|
|
|
// Stick it in the cache
|
|
|
|
|
|
|
|
StaticHandler.cache[filePath] = content; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Retrieve from memory cache
|
|
|
|
// Retrieve from memory cache
|
|
|
|
StaticHandler.prototype.serveCached = function(filePath, response) { |
|
|
|
StaticHandler.prototype.serveCached = function(filePath, callback) { |
|
|
|
var contentType = StaticHandler.contentTypeFor(path.extname(filePath)); |
|
|
|
callback(undefined, StaticHandler.cache[filePath]); |
|
|
|
response.writeHead(200, { 'content-type': contentType }); |
|
|
|
|
|
|
|
response.end(StaticHandler.cache[filePath], 'utf-8'); |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Determine if a given filePath is cached or not
|
|
|
|
|
|
|
|
StaticHandler.prototype.isCached = function(filePath) { |
|
|
|
|
|
|
|
return !!StaticHandler.cache[filePath]; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
module.exports = StaticHandler; |
|
|
|
module.exports = StaticHandler; |
|
|
|