diff --git a/TODO b/TODO index 3e355f3..a17a6f5 100644 --- a/TODO +++ b/TODO @@ -6,7 +6,6 @@ cache static in memory add feedback for errors to UI - esp. too long copy URL to clipboard button add about page -support built-in expiration # shared version only some way to do announcements easily (and use for ads) diff --git a/config.js b/config.js index 0b3def2..6fe6f8f 100644 --- a/config.js +++ b/config.js @@ -19,7 +19,8 @@ "type": "redis", "host": "localhost", "port": 6379, - "db": 2 + "db": 2, + "expire": 3600 } } diff --git a/lib/file_document_store.js b/lib/file_document_store.js index 5e0e1c9..03dd81e 100644 --- a/lib/file_document_store.js +++ b/lib/file_document_store.js @@ -4,6 +4,8 @@ var winston = require('winston'); var hashlib = require('hashlib'); // For storing in files +// options[type] = file +// options[path] - Where to store var FileDocumentStore = function(options) { this.basePath = options.path || './data'; diff --git a/lib/redis_document_store.js b/lib/redis_document_store.js index 2ba56dc..d63aded 100644 --- a/lib/redis_document_store.js +++ b/lib/redis_document_store.js @@ -2,7 +2,15 @@ var redis = require('redis'); var winston = require('winston'); var hashlib = require('hashlib'); +// For storing in redis +// options[type] = redis +// options[host] - The host to connect to (default localhost) +// options[port] - The port to connect to (default 5379) +// options[db] - The db to use (default 0) +// options[expire] - The time to live for each key set (default never) + var RedisDocumentStore = function(options) { + this.expire = options.expire; if (!RedisDocumentStore.client) { RedisDocumentStore.connect(options); } @@ -27,11 +35,31 @@ RedisDocumentStore.connect = function(options) { // Save file in a key RedisDocumentStore.prototype.set = function(key, data, callback) { + var _this = this; RedisDocumentStore.client.set(key, data, function(err, reply) { - callback(!err); + if (err) { + callback(false); + } + else { + _this.setExpiration(key); + callback(true); + } }); }; +// Expire a key in expire time if set +RedisDocumentStore.prototype.setExpiration = function(key) { + if (this.expire) { + RedisDocumentStore.client.expire(key, this.expire, function(err, reply) { + if (err || !reply) { + winston.error('failed to set expiry on key: ' + key); + } else { + console.log('set'); + } + }); + } +}; + // Get a file from a key RedisDocumentStore.prototype.get = function(key, callback) { RedisDocumentStore.client.get(key, function(err, reply) { diff --git a/server.js b/server.js index 49d8300..4425296 100644 --- a/server.js +++ b/server.js @@ -28,16 +28,14 @@ if (config.logging) { // build the store from the config on-demand - so that we don't load it // for statics -var preferredStore = function() { - if (!config.storage) { - config.storage = { type: 'file' }; - } - if (!config.storage.type) { - config.storage.type = 'file'; - } - var Store = require('./lib/' + config.storage.type + '_document_store'); - return new Store(config.storage); -}; +if (!config.storage) { + config.storage = { type: 'file' }; +} +if (!config.storage.type) { + config.storage.type = 'file'; +} +var Store = require('./lib/' + config.storage.type + '_document_store'); +var preferredStore = new Store(config.storage); // Set the server up and listen forever http.createServer(function(request, response) { @@ -48,7 +46,7 @@ http.createServer(function(request, response) { handler = new DocumentHandler({ keyLength: config.keyLength, maxLength: config.maxLength, - store: preferredStore() + store: preferredStore }); return handler.handlePost(request, response); } @@ -56,7 +54,7 @@ http.createServer(function(request, response) { var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/); if (request.method == 'GET' && match) { handler = new DocumentHandler({ - store: preferredStore() + store: preferredStore }); return handler.handleGet(match[1], response); }