1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 17:50:19 +01:00

Make static asset caching an option

This commit is contained in:
John Crepezzi 2011-11-21 23:00:28 -05:00
parent 1382ec47b2
commit 9ed330bdae
4 changed files with 12 additions and 7 deletions

2
TODO
View file

@ -3,8 +3,6 @@ tests
add FAVICON (or force 404) add FAVICON (or force 404)
add feedback for errors to UI - esp. too long add feedback for errors to UI - esp. too long
add about page add about page
make asset caching optional
todo store buffer instead of string while grabbing contents (if possible)
# shared version only # shared version only
some way to do announcements easily (and use for ads) some way to do announcements easily (and use for ads)

View file

@ -7,6 +7,8 @@
"maxLength": 400000, "maxLength": 400000,
"cacheStaticAssets": true,
"logging": [ "logging": [
{ {
"level": "verbose", "level": "verbose",

View file

@ -5,9 +5,10 @@ var winston = require('winston');
// For serving static assets // For serving static assets
var StaticHandler = function(path) { var StaticHandler = function(path, cacheAssets) {
this.basePath = path; this.basePath = path;
this.defaultPath = '/index.html'; this.defaultPath = '/index.html';
this.cacheAssets = cacheAssets;
// Grab the list of available files - and move into hash for quick lookup // Grab the list of available files - and move into hash for quick lookup
var available = fs.readdirSync(this.basePath); var available = fs.readdirSync(this.basePath);
this.availablePaths = {}; this.availablePaths = {};
@ -36,9 +37,10 @@ StaticHandler.prototype.handle = function(incPath, response) {
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 - either from the cache or from source // And then stream the file back - either from the cache or from source
var cached = this.isCached(filePath); var cached = this.cacheAssets && this.isCached(filePath);
var method = cached ? this.serveCached : this.retrieve; var method = cached ? this.serveCached : this.retrieve;
// Run! // Run!
var _this = this;
method(filePath, function(error, content) { method(filePath, function(error, content) {
// Get the content // Get the content
if (content) { if (content) {
@ -46,7 +48,7 @@ StaticHandler.prototype.handle = function(incPath, response) {
response.writeHead(200, { 'content-type': contentType }); response.writeHead(200, { 'content-type': contentType });
response.end(content, 'utf-8'); response.end(content, 'utf-8');
// Stick it in the cache if its not in there // Stick it in the cache if its not in there
if (!cached) { if (!cached && _this.cacheAssets) {
StaticHandler.cache[filePath] = content; StaticHandler.cache[filePath] = content;
} }
} }
@ -66,6 +68,7 @@ StaticHandler.prototype.handle = function(incPath, response) {
// Retrieve from the file // Retrieve from the file
StaticHandler.prototype.retrieve = function(filePath, callback) { StaticHandler.prototype.retrieve = function(filePath, callback) {
var _this = this; var _this = this;
winston.verbose('loading static asset', { path: filePath });
fs.readFile(filePath, function(error, content) { fs.readFile(filePath, function(error, content) {
callback(error, content); callback(error, content);
}); });

View file

@ -37,6 +37,9 @@ if (!config.storage.type) {
var Store = require('./lib/' + config.storage.type + '_document_store'); var Store = require('./lib/' + config.storage.type + '_document_store');
var preferredStore = new Store(config.storage); var preferredStore = new Store(config.storage);
// Configure a static handler for the static files
var staticHandler = new StaticHandler('./static', !!config.cacheStaticAssets);
// Set the server up and listen forever // Set the server up and listen forever
http.createServer(function(request, response) { http.createServer(function(request, response) {
var incoming = url.parse(request.url, false); var incoming = url.parse(request.url, false);
@ -59,8 +62,7 @@ http.createServer(function(request, response) {
return handler.handleGet(match[1], response); return handler.handleGet(match[1], response);
} }
// Otherwise, look for static file // Otherwise, look for static file
handler = new StaticHandler('./static'); staticHandler.handle(incoming.pathname, response);
handler.handle(incoming.pathname, response);
}).listen(config.port, config.host); }).listen(config.port, config.host);
winston.info('listening on ' + config.host + ':' + config.port); winston.info('listening on ' + config.host + ':' + config.port);