From 9ed330bdae4f8dfc40898c88ed6449ef2150769d Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Mon, 21 Nov 2011 23:00:28 -0500 Subject: [PATCH] Make static asset caching an option --- TODO | 2 -- config.js | 2 ++ lib/static_handler.js | 9 ++++++--- server.js | 6 ++++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/TODO b/TODO index 122ad6e..a9608eb 100644 --- a/TODO +++ b/TODO @@ -3,8 +3,6 @@ tests add FAVICON (or force 404) add feedback for errors to UI - esp. too long add about page -make asset caching optional -todo store buffer instead of string while grabbing contents (if possible) # shared version only some way to do announcements easily (and use for ads) diff --git a/config.js b/config.js index 6fe6f8f..f849e89 100644 --- a/config.js +++ b/config.js @@ -7,6 +7,8 @@ "maxLength": 400000, + "cacheStaticAssets": true, + "logging": [ { "level": "verbose", diff --git a/lib/static_handler.js b/lib/static_handler.js index 9701a7c..53a34f7 100644 --- a/lib/static_handler.js +++ b/lib/static_handler.js @@ -5,9 +5,10 @@ var winston = require('winston'); // For serving static assets -var StaticHandler = function(path) { +var StaticHandler = function(path, cacheAssets) { this.basePath = path; this.defaultPath = '/index.html'; + this.cacheAssets = cacheAssets; // Grab the list of available files - and move into hash for quick lookup var available = fs.readdirSync(this.basePath); this.availablePaths = {}; @@ -36,9 +37,10 @@ StaticHandler.prototype.handle = function(incPath, response) { if (!this.availablePaths[incPath]) incPath = this.defaultPath; var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); // 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; // Run! + var _this = this; method(filePath, function(error, content) { // Get the content if (content) { @@ -46,7 +48,7 @@ StaticHandler.prototype.handle = function(incPath, response) { response.writeHead(200, { 'content-type': contentType }); response.end(content, 'utf-8'); // Stick it in the cache if its not in there - if (!cached) { + if (!cached && _this.cacheAssets) { StaticHandler.cache[filePath] = content; } } @@ -66,6 +68,7 @@ StaticHandler.prototype.handle = function(incPath, response) { // Retrieve from the file StaticHandler.prototype.retrieve = function(filePath, callback) { var _this = this; + winston.verbose('loading static asset', { path: filePath }); fs.readFile(filePath, function(error, content) { callback(error, content); }); diff --git a/server.js b/server.js index 4425296..a747580 100644 --- a/server.js +++ b/server.js @@ -37,6 +37,9 @@ if (!config.storage.type) { var Store = require('./lib/' + config.storage.type + '_document_store'); 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 http.createServer(function(request, response) { var incoming = url.parse(request.url, false); @@ -59,8 +62,7 @@ http.createServer(function(request, response) { return handler.handleGet(match[1], response); } // Otherwise, look for static file - handler = new StaticHandler('./static'); - handler.handle(incoming.pathname, response); + staticHandler.handle(incoming.pathname, response); }).listen(config.port, config.host); winston.info('listening on ' + config.host + ':' + config.port);