Skip to content

Commit

Permalink
- add output filters
Browse files Browse the repository at this point in the history
  • Loading branch information
hildoer committed Dec 3, 2012
1 parent 989835d commit 4c4fcd3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 41 deletions.
12 changes: 7 additions & 5 deletions index.js
Expand Up @@ -15,13 +15,14 @@ module.exports.run = function ( conf ) {
running = true;

conf = {
server: {
server: {
port: 8080
},
storage: {
storage: {
host: 'localhost',
port: 6379
}
},
outputFilters: {}
}.mixin( conf );

storage.init( conf.storage, function ( err ) {
Expand All @@ -32,8 +33,9 @@ module.exports.run = function ( conf ) {
}
else {
core.init( {
express: express,
storage: storage
express: express,
storage: storage,
outputFilters: conf.outputFilters
}, function ( err ) {
if ( err ) {
console.error( 'could not start up core' );
Expand Down
95 changes: 59 additions & 36 deletions lib/core/index.js
Expand Up @@ -6,8 +6,18 @@ var moduleConfig = null;
module.exports.init = function ( config, callback ) {

moduleConfig = {
express: null,
storage: null
express: null,
storage: null,
outputFilters: {
json: function ( conf, callback ) {
callback( JSON.stringify( conf ), "application/json; charset=utf-8" );
},
xml: function ( conf, callback ) {

// TODO, fix this!!!
callback( "You should have XML here!" );
}
}
}.mixin( config );

if ( !config.express ) {
Expand Down Expand Up @@ -48,7 +58,7 @@ function checkAuth( req, res, next ) {
function onPostConf( req, res ) {

res = getResponder( req, res );
var path = getPath( req );
var path = req.confPath;

if ( !path ) {
res( 400 );
Expand All @@ -69,7 +79,7 @@ function onPostConf( req, res ) {
function onGetConf( req, res ) {

res = getResponder( req, res );
var path = getPath( req );
var path = req.confPath;

if ( !path ) {
res( 400 );
Expand All @@ -85,12 +95,29 @@ function onGetConf( req, res ) {
}

getConf( path, environment, function ( conf ) {

if ( conf === null ) {
res( 404 );
}
else {
res( 200, conf );

try {

if ( typeof moduleConfig.outputFilters[req.outputFilter] === 'function' ) {
moduleConfig.outputFilters[req.outputFilter]( conf, function ( conf, contentType ) {
res( 200, conf, contentType );
} );
}
else {
res( 500 );
}

}
catch ( e ) {
res( 500 );
}
}

} );

}
Expand Down Expand Up @@ -266,7 +293,7 @@ function applyAbstractions( conf, environment, context, callback ) {

function onDeleteConf( req, res ) {
res = getResponder( req, res );
var path = getPath( req );
var path = req.confPath;

if ( !path ) {
res( 400 );
Expand Down Expand Up @@ -314,17 +341,31 @@ function onDeleteAuth( req, res ) {
* Get the conf path from the URL path.
*
* @param req The express request handle
* @return {string} The conf path
* @param res The response object passed to the middleware
* @param next The next function to trigger the next middleware
* @return {null}
*/
function getPath( req ) {
function getPath( req, res, next ) {

var path = req.path.replace( /\//g, '.' ).replace( /^\.conf/, '' ).replace( /\.$/, '' );
var path = req.path.replace( /\..*$/, '' ).replace( /\//g, '.' ).replace( /^\.conf/, '' ).replace( /\.$/, '' ).trim();

if ( path.length < 1 ) {
return null;
path = null;
}

var outputFilter = req.path.trim().match( /\.(.*)$/ );
if ( outputFilter ) {
outputFilter = outputFilter[1].trim();
}

return path;
if ( typeof outputFilter !== 'string' || outputFilter.length < 1 ) {
outputFilter = 'json';
}

req.confPath = path;
req.outputFilter = outputFilter;

return next();

}

Expand All @@ -350,9 +391,9 @@ function storeRequestBody( req, res, next ) {
function configureExpress() {

// create configuration routes
moduleConfig.express.get( /^\/conf.*/, checkAuth, getMiddlewareWrapper( onGetConf ) );
moduleConfig.express.post( /^\/conf.*/, storeRequestBody, checkAuth, getMiddlewareWrapper( onPostConf ) );
moduleConfig.express.delete( /^\/conf.*/, checkAuth, getMiddlewareWrapper( onDeleteConf ) );
moduleConfig.express.get( /^\/conf.*/, checkAuth, getPath, getMiddlewareWrapper( onGetConf ) );
moduleConfig.express.post( /^\/conf.*/, storeRequestBody, checkAuth, getPath, getMiddlewareWrapper( onPostConf ) );
moduleConfig.express.delete( /^\/conf.*/, checkAuth, getPath, getMiddlewareWrapper( onDeleteConf ) );

// create auth management routes
moduleConfig.express.post( "/auth", checkAuth, getMiddlewareWrapper( onPostAuth ) );
Expand Down Expand Up @@ -383,35 +424,17 @@ function getMiddlewareWrapper( middleware ) {
* @param res A handle to a response object to send the response to when the responder function is called
* @return {Function} The responder function for middleware to call to send a response to a request
*/
function getResponder( req, res, format ) {
function getResponder( req, res ) {

if ( !format ) {
format = 'json';
}

return function ( code, body ) {

var contentType = null;
return function ( code, body, contentType ) {

if ( code !== 200 ) {
body = "";
contentType = "text/html; charset=utf-8";
}
else if ( typeof body !== "string" ) {
switch ( format ) {
case 'xml':
code = 400;
body = "";
contentType = "text/html; charset=utf-8";
break;
case 'json':
body = JSON.stringify( body );
contentType = "application/json; charset=utf-8";
break;
default:
break;
}

if ( !contentType ) {
contentType = "text/html; charset=utf-8";
}

res.writeHead( code, {
Expand Down

0 comments on commit 4c4fcd3

Please sign in to comment.