Loading TODO +0 −2 Original line number Diff line number Diff line Loading @@ -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) Loading config.js +2 −0 Original line number Diff line number Diff line Loading @@ -7,6 +7,8 @@ "maxLength": 400000, "cacheStaticAssets": true, "logging": [ { "level": "verbose", Loading lib/static_handler.js +6 −3 Original line number Diff line number Diff line Loading @@ -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 = {}; Loading Loading @@ -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) { Loading @@ -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; } } Loading @@ -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); }); Loading server.js +4 −2 Original line number Diff line number Diff line Loading @@ -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); Loading @@ -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); Loading
TODO +0 −2 Original line number Diff line number Diff line Loading @@ -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) Loading
config.js +2 −0 Original line number Diff line number Diff line Loading @@ -7,6 +7,8 @@ "maxLength": 400000, "cacheStaticAssets": true, "logging": [ { "level": "verbose", Loading
lib/static_handler.js +6 −3 Original line number Diff line number Diff line Loading @@ -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 = {}; Loading Loading @@ -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) { Loading @@ -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; } } Loading @@ -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); }); Loading
server.js +4 −2 Original line number Diff line number Diff line Loading @@ -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); Loading @@ -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);