From e403549e132a95a93b3c7f78eb1b03e05b6839aa Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 22 Nov 2011 09:22:37 -0500 Subject: [PATCH] Added static document loading on start --- TODO | 2 ++ about.md | 5 +++++ config.js | 4 ++++ lib/redis_document_store.js | 6 ++++-- server.js | 34 ++++++++++++++++++++++++---------- 5 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 about.md diff --git a/TODO b/TODO index 648d12e..e70f983 100644 --- a/TODO +++ b/TODO @@ -2,6 +2,8 @@ cache headers for static assets (and on document GET) tests add feedback for errors to UI - esp. too long add about page +fix that chrome bug where it loads the doc twice +add link to about page # shared version only some way to do announcements easily (and use for ads) diff --git a/about.md b/about.md new file mode 100644 index 0000000..67e5e8f --- /dev/null +++ b/about.md @@ -0,0 +1,5 @@ +# Haste + +Sharing code is a good thing, and it should be _really_ easy to do it. A lot of times, I want to show you something I'm seeing - and that's where we use pastebins. + +Haste is the prettiest, easist to use pastebin every made. diff --git a/config.js b/config.js index f849e89..22ff245 100644 --- a/config.js +++ b/config.js @@ -23,6 +23,10 @@ "port": 6379, "db": 2, "expire": 3600 + }, + + "documents": { + "about": "./about.md" } } diff --git a/lib/redis_document_store.js b/lib/redis_document_store.js index cf0a425..163b397 100644 --- a/lib/redis_document_store.js +++ b/lib/redis_document_store.js @@ -34,14 +34,16 @@ RedisDocumentStore.connect = function(options) { }; // Save file in a key -RedisDocumentStore.prototype.set = function(key, data, callback) { +RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) { var _this = this; RedisDocumentStore.client.set(key, data, function(err, reply) { if (err) { callback(false); } else { - _this.setExpiration(key); + if (!skipExpire) { + _this.setExpiration(key); + } callback(true); } }); diff --git a/server.js b/server.js index a747580..0b95b33 100644 --- a/server.js +++ b/server.js @@ -37,29 +37,43 @@ if (!config.storage.type) { var Store = require('./lib/' + config.storage.type + '_document_store'); var preferredStore = new Store(config.storage); +// Send the static documents into the preferred store, skipping expirations +for (var name in config.documents) { + var path = config.documents[name]; + fs.readFile(path, 'utf8', function(err, data) { + if (data && !err) { + preferredStore.set(name, data, function(cb) { + winston.info('loaded static document', { name: name, path: path }); + }, true); + } + else { + winston.warn('failed to load static document', { name: name, path: path }); + } + }); +} + // Configure a static handler for the static files var staticHandler = new StaticHandler('./static', !!config.cacheStaticAssets); +// Configure the document handler +var documentHandler = new DocumentHandler({ + store: preferredStore, + maxLength: config.maxLength, + keyLength: config.keyLength +}); + // Set the server up and listen forever http.createServer(function(request, response) { var incoming = url.parse(request.url, false); var handler = null; // Looking to add a new doc if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') { - handler = new DocumentHandler({ - keyLength: config.keyLength, - maxLength: config.maxLength, - store: preferredStore - }); - return handler.handlePost(request, response); + return documentHandler.handlePost(request, response); } // Looking up a doc var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/); if (request.method == 'GET' && match) { - handler = new DocumentHandler({ - store: preferredStore - }); - return handler.handleGet(match[1], response); + return documentHandler.handleGet(match[1], response); } // Otherwise, look for static file staticHandler.handle(incoming.pathname, response);