From b159817a2711207c204e2d3af054fdcf6bc0be0a Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Fri, 18 Nov 2011 16:54:16 -0500 Subject: [PATCH] Added configuration and made key length configurable --- config.js | 5 +++++ lib/document_handler.js | 7 +++---- server.js | 9 ++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 config.js diff --git a/config.js b/config.js new file mode 100644 index 0000000..887ee1f --- /dev/null +++ b/config.js @@ -0,0 +1,5 @@ +{ + + "keyLength": 6 + +} diff --git a/lib/document_handler.js b/lib/document_handler.js index 48bff43..3ed0637 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -2,8 +2,8 @@ var winston = require('winston'); // For handling serving stored documents -var DocumentHandler = function() { - +var DocumentHandler = function(options) { + this.keyLength = options.keyLength || 20; }; // TODO implement with FS backend @@ -45,11 +45,10 @@ DocumentHandler.prototype.handlePost = function(request, response) { }; // Generate a random key -// TODO make length configurable DocumentHandler.prototype.randomKey = function() { var text = ''; var keyspace = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - for (var i = 0; i < 6; i++) { + for (var i = 0; i < this.keyLength; i++) { text += keyspace.charAt(Math.floor(Math.random() * keyspace.length)); } return text; diff --git a/server.js b/server.js index 7a586ef..faf29e9 100644 --- a/server.js +++ b/server.js @@ -1,13 +1,16 @@ var http = require('http'); var url = require('url'); +var fs = require('fs'); var winston = require('winston'); var StaticHandler = require('./lib/static_handler'); var DocumentHandler = require('./lib/document_handler'); -///////////// -// Configure logging TODO +// Load the configuration +var config = JSON.parse(fs.readFileSync('config.js', 'utf8')); + +// Configure logging - TODO make configurable winston.remove(winston.transports.Console); winston.add(winston.transports.Console, { colorize: true, level: 'verbose' }); @@ -22,7 +25,7 @@ http.createServer(function(request, response) { // Looking to add a new doc if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') { - handler = new DocumentHandler(); + handler = new DocumentHandler({ keyLength: config.keyLength }); return handler.handlePost(request, response); }