connect-rest
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -12,5 +12,5 @@ function Bus(httphelper) { | ||
busPrototype.initBus = function( options, logger ) { | ||
busPrototype.initBus = function( options, log ) { | ||
if( options && options.populateInterval ){ | ||
this.logger = logger; | ||
this.log = log; | ||
@@ -49,3 +49,3 @@ var self = this; | ||
this.logger.debug('Populating monitoring results...' ); | ||
this.log( null, 'Populating monitoring results...', {} ); | ||
@@ -52,0 +52,0 @@ if(this.options.listener){ |
@@ -6,3 +6,2 @@ /* | ||
*/ | ||
var VERSION = '0.7.6'; | ||
@@ -42,4 +41,8 @@ var connect = require('connect'); | ||
var VERSION = exports.VERSION = '1.4.0'; | ||
var winston = require('winston'); | ||
var logger; | ||
var log = function( err, message, obj, level ){ | ||
logger.log( err ? 'error' : (level || 'debug'), err ? err.message : message, _.extend( obj || {}, { 'connect-rest': VERSION } ) ); | ||
}; | ||
@@ -137,3 +140,3 @@ var toString = Object.prototype.toString; | ||
function processRequest(req, res, matching, bodyObj){ | ||
logger.debug('Payload object: ', bodyObj ); | ||
log( null, 'Process request', { body: bodyObj }, 'verbose' ); | ||
@@ -151,4 +154,2 @@ var asyncCall = req.query.callbackURL; | ||
logger.debug('Calling service functions.' ); | ||
req.headers.clientAddress = (req.headers['x-forwarded-for'] || '').split(',')[0] || req.connection.remoteAddress; | ||
@@ -158,8 +159,6 @@ | ||
function(err, results){ | ||
logger.info( 'Service(s) calling finished.', err, results ); | ||
log( err, 'Results', { results: results }, 'verbose' ); | ||
var result; | ||
if( err ) | ||
logger.error( err ); | ||
else | ||
if( !err ) | ||
result = _.find(results, function(returnValue){ return returnValue && returnValue.result; }) || { result: '', resOptions:{ statusCode: 204 } }; | ||
@@ -169,6 +168,3 @@ | ||
httphelper.generalCall( asyncCall, 'POST', null, err, result, result.contentType || 'application/json', logger, function(err, result, status){ | ||
if(err) | ||
logger.error( err ); | ||
else | ||
logger.info('Response:', result, status); | ||
log( err, 'Async response sent.', { result: result, status: status }, 'verbose' ); | ||
} ); | ||
@@ -247,7 +243,8 @@ } else{ | ||
addPath("DELETE", path, functionRef, prototypeObject, options ); | ||
else logger.error('Not known rest type:', element); | ||
else | ||
throw new Error('Not known rest type:', element); | ||
} ); | ||
} | ||
else | ||
logger.error('Not correct given methods', methods); | ||
throw new Error('Not correct given methods', methods); | ||
}; | ||
@@ -309,3 +306,3 @@ exports.proxy = function proxyRest( method, path, remotePath, options ){ | ||
bus.shutdown(); | ||
logger.info('Halting connect-rest.'); | ||
log( null, 'Halting connect-rest.', { }, 'verbose' ); | ||
}; | ||
@@ -316,22 +313,19 @@ | ||
logger = winston; | ||
options = options || {}; | ||
if( options ){ | ||
exports.context( options.context ? options.context : (options.context === '' ? '' : '/api') ); | ||
exports.context( options.context ? options.context : (options.context === '' ? '' : '/api') ); | ||
if( options.discoverPath ) | ||
addPath('GET', options.discoverPath + '/:version', discover ); | ||
if( options.protoPath ) | ||
addPath('GET', options.protoPath + '/*path', protoPather ); | ||
API_KEYS = options.apiKeys; | ||
if( options.discoverPath ) | ||
addPath('GET', options.discoverPath + '/:version', discover ); | ||
if( options.protoPath ) | ||
addPath('GET', options.protoPath + '/*path', protoPather ); | ||
API_KEYS = options.apiKeys; | ||
if( options.logger ) | ||
logger = options.logger.info ? options.logger : exports.setupLogger( options.logger, logger ); | ||
logger = options.logger ? ( options.logger.level ? exports.createWinstonLogger( options.logger ) : options.logger ) : exports.createWinstonLogger( {} ); | ||
domain = options.domain; | ||
domain = options.domain; | ||
bus.initBus( options.monitoring, logger ); | ||
bus.initBus( options.monitoring, log ); | ||
logger.info('connect-rest has been configured. ', options); | ||
} | ||
log( null, 'connect-rest has been configured.', { options: options }, 'verbose' ); | ||
@@ -342,2 +336,4 @@ return function restMaker(req, res, next) { | ||
var pathname = url.parse( req.url ).pathname; | ||
if( domain ){ | ||
@@ -354,3 +350,3 @@ domain.add(req); | ||
} catch (er) { | ||
logger.error('Error sending 500', er, req.url); | ||
log( er, '', { pathname: pathname } ); | ||
domain.dispose(); | ||
@@ -361,8 +357,4 @@ } | ||
logger.info('Incoming request.', req.headers, req.query, req.httpVersion, req.method, req.originalUrl ); | ||
log( null, 'Incoming request.', { headers: req.headers, query: req.query, httpVersion: req.httpVersion, method: req.method, originalUrl: req.originalUrl, pathname: pathname }, 'verbose' ); | ||
var pathname = url.parse( req.url ).pathname; | ||
logger.debug('Request received from: ', pathname ); | ||
var apiKey = req.headers['api-key'] || req.headers['x-api-key'] || req.query.api_key; | ||
@@ -376,3 +368,3 @@ | ||
logger.debug('Routes matching: ', routes ); | ||
log( null, 'Routes matching.', { routes: routes }, 'verbose' ); | ||
@@ -383,3 +375,3 @@ var matching = _.map( routes, function(route){ return route.maction; } ); | ||
if( API_KEYS && API_KEYS.indexOf( apiKey ) === -1 ){ | ||
logger.info('Request without api key.', req.query ); | ||
log( null, 'Request without api key.', { pathname: pathname }, 'verbose' ); | ||
@@ -391,3 +383,3 @@ res.statusCode = 401; | ||
else { | ||
logger.info('Request won\'t be handled by connect-rest.', req.url ); | ||
log( null, 'Request won\'t be handled by connect-rest.', { pathname: pathname }, 'verbose' ); | ||
@@ -402,20 +394,18 @@ return next(); | ||
exports.createWinstonLogger = function( options ){ | ||
return new (winston.Logger)({ | ||
transports: [ | ||
new (winston.transports.Console)({ level: 'error' }), | ||
new (winston.transports.File)({ | ||
filename: options.file || 'server.log', | ||
level: options.level || 'info' | ||
}) | ||
] | ||
}); | ||
options = options || {}; | ||
if( options.exceptionFile ) | ||
winston.handleExceptions(new winston.transports.File({ filename: options.exceptionFile })); | ||
var transports = [ | ||
new (winston.transports.Console)({ level: 'error', colorize: 'true' }), | ||
new (winston.transports.File)( { | ||
filename: options.file || 'server.log', | ||
level: options.level || 'info', | ||
maxsize: options.maxSize || 1000000, | ||
maxFiles: options.maxFiles || 1 | ||
} ) | ||
]; | ||
return new (winston.Logger)({ transports: transports }); | ||
}; | ||
exports.setupLogger = function(options, _logger){ | ||
return options ? exports.createWinstonLogger( options ) : (_logger ? _logger : winston); | ||
}; | ||
exports.dispatcher = function( method, path, handler ){ | ||
@@ -425,4 +415,3 @@ return Dispatcher.dispatch( method, path, url, _, semver, handler ); | ||
exports.VERSION = VERSION; | ||
exports.CONTEXT = CONTEXT; | ||
exports.SERVICE_METHOD_PATTERN = SERVICE_METHOD_PATTERN; |
{ | ||
"name": "connect-rest", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Exceptionally featureful RESTful web services middleware for Connect.", | ||
@@ -57,3 +57,3 @@ "keywords": [ | ||
}, | ||
"_id": "connect-rest@1.3.1" | ||
"_id": "connect-rest@1.4.0" | ||
} |
Sorry, the diff of this file is not supported yet
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
24
1197865
1232