connect-rest
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -6,3 +6,3 @@ /* | ||
*/ | ||
var VERSION = '0.0.5'; | ||
var VERSION = '0.0.6'; | ||
@@ -13,2 +13,3 @@ var util = require('util'); | ||
var semver = require('semver'); | ||
var bunyan = require('bunyan'); | ||
var _ = require('underscore'); | ||
@@ -35,16 +36,9 @@ _.str = require('underscore.string'); | ||
var logger; | ||
var LOGGER_ENTITY = { | ||
'logger':'Rest', | ||
'context': CONTEXT | ||
} | ||
function debug( message, object ){ | ||
if(logger) | ||
logger.debug( LOGGER_ENTITY, message, object ); | ||
} | ||
function trace( message, object ){ | ||
if(logger) | ||
logger.trace( LOGGER_ENTITY, message, object ); | ||
} | ||
function DummyLogger(){ }; | ||
DummyLogger.prototype.info = function() { }; | ||
DummyLogger.prototype.debug = function() { }; | ||
function addPath(key, path, action){ | ||
@@ -92,3 +86,3 @@ mapping[ key ].push( new Route( path, | ||
exports.context = function (context){ | ||
LOGGER_ENTITY.context = CONTEXT = context; | ||
CONTEXT = context; | ||
} | ||
@@ -101,3 +95,8 @@ | ||
API_KEYS = options.apiKeys; | ||
logger = options.logger; | ||
logger = new DummyLogger(); | ||
if( options.logger ) | ||
logger = _.isString( options.logger ) ? bunyan.createLogger({name: options.logger, src: true}) : options.logger; | ||
logger.info('connect-rest has been configured. ', options); | ||
} | ||
@@ -108,3 +107,7 @@ | ||
logger.info('Incoming request.', req.headers, req.query ); | ||
if( API_KEYS && API_KEYS.indexOf(req.query.api_key)==-1 ){ | ||
logger.info('Request without api key.', req.query ); | ||
res.statusCode = 401; | ||
@@ -116,13 +119,20 @@ res.end( 'API_KEY is required.' ); | ||
var pathname = url.parse( req.url ).pathname; | ||
var matching = _.map( _.filter( | ||
logger.debug('Request received from: ', pathname ); | ||
var routes = _.filter( | ||
mapping[ req.method ], function(route){ return route.matches( | ||
req, pathname, CONTEXT, req.headers['accept-version'] || req.headers['x-api-version'] || '*', _, semver | ||
); } | ||
), | ||
function(route){ return route.action; } | ||
); | ||
); | ||
var matching = _.map( routes, function(route){ return route.action; } ); | ||
if( matching.length == 0 ) | ||
if( matching.length == 0 ){ | ||
logger.info('Request won\'t be handled by connect-rest.', req.url ); | ||
return next(); | ||
} | ||
logger.debug('Routes matching: ', routes ); | ||
var body = ''; | ||
@@ -137,8 +147,16 @@ req.on('data', function(chunk) { | ||
req.on('end', function() { | ||
logger.debug('Body payload: ', body ); | ||
var bodyObj = body.length > 0 ? JSON.parse( body ) : ''; | ||
logger.debug('Parsed JSON object: ', bodyObj ); | ||
var callChain = _.map( matching, function(func){ return async.apply( func, {headers: req.headers, parameters: req.query}, bodyObj ); } ); | ||
logger.debug('Calling service functions.' ); | ||
async.series( callChain, | ||
function(err, results){ | ||
logger.info('Service(s) calling finished.', err, results ); | ||
if( err ){ | ||
@@ -163,3 +181,1 @@ res.statusCode = 500; | ||
exports.ERROR_MESSAGE = ERROR_MESSAGE; | ||
exports.LOGGER_ENTITY = LOGGER_ENTITY; | ||
{ | ||
"name": "connect-rest", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "RESTful web services middleware for Connect.", | ||
@@ -33,3 +33,4 @@ "keywords": [ | ||
"underscore.string":"~2", | ||
"semver": "~1" | ||
"semver": "~1", | ||
"bunyan": ">=0.18.0" | ||
}, | ||
@@ -36,0 +37,0 @@ "devDependencies":{ |
@@ -136,3 +136,26 @@ [connect-rest](https://github.com/imrefazekas/connect-rest) is a middleware for [connect](http://www.senchalabs.org/connect/) for building REST APIs providing service discovery and path-based parameter mapping as well. | ||
## Logging | ||
In the option object passed to the constructor, there is an optional parameter 'logger', which enables the logging functionality: | ||
var options = { | ||
'apiKeys': [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
'discoverPath': 'discover', | ||
'logger': 'connect-rest' | ||
}; | ||
or | ||
var options = { | ||
'apiKeys': [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
'discoverPath': 'discover', | ||
'logger': loggerInstance | ||
}; | ||
You can set: | ||
- a string, which will be interpret as the name of the logger seen in the logs, or | ||
- passing a bunyan instance to be used. | ||
In the absence of 'logger' property, no logs will be made. | ||
The connect-rest will use level 'info' for entry and exit points of services and 'debug' for the milestones of all internal processes. | ||
## Server - extracted from the tests | ||
@@ -194,6 +217,7 @@ | ||
- logging services should be added properly | ||
- more detailed examples | ||
## Changelog | ||
- 0.0.6 : logging added | ||
- 0.0.5 : optional parameter added | ||
@@ -200,0 +224,0 @@ - 0.0.4 : API_KEY management added |
@@ -19,3 +19,4 @@ var rest = require('../lib/connect-rest'); | ||
'apiKeys': [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
'discoverPath': 'discover' | ||
'discoverPath': 'discover', | ||
'logger': 'connect-rest' | ||
}; | ||
@@ -22,0 +23,0 @@ connectApp.use( rest.rester( options ) ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22576
363
225
5
+ Addedbunyan@>=0.18.0
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbunyan@2.0.5(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddtrace-provider@0.8.8(transitive)
+ Addedexeunt@1.1.0(transitive)
+ Addedglob@6.0.4(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedmv@2.1.1(transitive)
+ Addednan@2.22.0(transitive)
+ Addedncp@2.0.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedrimraf@2.4.5(transitive)
+ Addedsafe-json-stringify@1.2.0(transitive)
+ Addedwrappy@1.0.2(transitive)