1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 17:50:19 +01:00

Generalize store

This commit is contained in:
John Crepezzi 2011-11-18 18:04:24 -05:00
parent fec02cfead
commit 83b8f3f52d
3 changed files with 24 additions and 14 deletions

View file

@ -11,6 +11,11 @@
"type": "Console", "type": "Console",
"colorize": true "colorize": true
} }
] ],
"storage": {
"type": "file",
"path": "./data"
}
} }

View file

@ -4,11 +4,9 @@ var winston = require('winston');
var hashlib = require('hashlib'); var hashlib = require('hashlib');
// For storing in files // For storing in files
// TODO make data path configurable
// TODO make store type configurable
var FileDocumentStore = function(path) { var FileDocumentStore = function(options) {
this.basePath = path; this.basePath = options.path || './data';
}; };
// Save data in a file, key as md5 - since we don't know what we could be passed here // Save data in a file, key as md5 - since we don't know what we could be passed here

View file

@ -6,7 +6,6 @@ var winston = require('winston');
var StaticHandler = require('./lib/static_handler'); var StaticHandler = require('./lib/static_handler');
var DocumentHandler = require('./lib/document_handler'); var DocumentHandler = require('./lib/document_handler');
var FileDocumentStore = require('./lib/file_document_store');
// Load the configuration and set some defaults // Load the configuration and set some defaults
var config = JSON.parse(fs.readFileSync('config.js', 'utf8')); var config = JSON.parse(fs.readFileSync('config.js', 'utf8'));
@ -29,34 +28,42 @@ if (config.logging) {
// TODO implement command line // TODO implement command line
// Set the server up // build the store from the config on-demand - so that we don't load it
http.createServer(function(request, response) { // 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);
};
// Set the server up and listen forever
http.createServer(function(request, response) {
var incoming = url.parse(request.url, false); var incoming = url.parse(request.url, false);
var handler = null; var handler = null;
// Looking to add a new doc // Looking to add a new doc
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') { if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
handler = new DocumentHandler({ handler = new DocumentHandler({
keyLength: config.keyLength, keyLength: config.keyLength,
store: new FileDocumentStore('./data') store: preferredStore()
}); });
return handler.handlePost(request, response); return handler.handlePost(request, response);
} }
// Looking up a doc // Looking up a doc
var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/); var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/);
if (request.method == 'GET' && match) { if (request.method == 'GET' && match) {
handler = new DocumentHandler({ handler = new DocumentHandler({
store: new FileDocumentStore('./data') store: preferredStore()
}); });
return handler.handleGet(match[1], response); return handler.handleGet(match[1], response);
} }
// Otherwise, look for static file // Otherwise, look for static file
handler = new StaticHandler('./static'); handler = new StaticHandler('./static');
handler.handle(incoming.pathname, response); handler.handle(incoming.pathname, response);
}).listen(config.port, config.host); }).listen(config.port, config.host);
winston.info('listening on ' + config.host + ':' + config.port); winston.info('listening on ' + config.host + ':' + config.port);