connect-rest
Advanced tools
Comparing version 0.0.41 to 0.0.42
module.exports = function(grunt) { | ||
var rest = require('./lib/connect-rest'); | ||
var restBuilder = require('./test/restBuilder'); | ||
grunt.loadNpmTasks('grunt-contrib-jshint'); | ||
grunt.loadNpmTasks('grunt-contrib-connect'); | ||
grunt.loadNpmTasks('grunt-contrib-nodeunit'); | ||
@@ -7,6 +12,34 @@ grunt.initConfig({ | ||
all: [ 'lib/*.js', 'lib/**/*.js', '!lib/db/connector.js' ] | ||
}, | ||
connect: { | ||
server: { | ||
options: { | ||
port: 8080, | ||
middleware: function(connect, options) { | ||
var restOptions = { | ||
apiKeys: [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
discoverPath: 'discover', | ||
protoPath: 'proto', | ||
logger: 'connect-rest', | ||
logLevel: 'debug', | ||
context: '/api' | ||
}; | ||
var middlewares = [ | ||
connect.query(), | ||
rest.rester( restOptions ) | ||
]; | ||
restBuilder.buildUpRestAPI( rest ); | ||
return middlewares; | ||
} | ||
} | ||
} | ||
}, | ||
nodeunit: { | ||
tests: ['test/nodeunit/caller.js'] | ||
} | ||
}); | ||
grunt.registerTask('default', 'jshint'); | ||
grunt.registerTask('test', ['connect', 'nodeunit']); | ||
grunt.registerTask('default', ['jshint', 'test']); | ||
}; |
@@ -1,5 +0,9 @@ | ||
function Bus(httphelper, _) { | ||
var newrelic = require('./newrelic'); | ||
function Bus(httphelper) { | ||
this.httphelper = httphelper; | ||
this._ = _; | ||
this.metrics = { }; | ||
this.maxCallCount = 0; | ||
this.maxDuration = 0; | ||
} | ||
@@ -15,16 +19,32 @@ | ||
var self = this; | ||
self.lastPopulation = Date.now(); | ||
self.intervalId = setInterval( function(){ | ||
self.populateMetrics(); | ||
self.lastPopulation = Date.now(); | ||
}, options.populateInterval ); | ||
this.options = options; | ||
if(options.newrelic){ | ||
options.newrelic.version = options.newrelic.version || options.version; | ||
options.newrelic.host = options.newrelic.host || options.host; | ||
this.newrelic = new newrelic( options.newrelic, this.httphelper, logger ); | ||
} | ||
} | ||
}; | ||
busPrototype.reportCall = function( callpath, routes, duration ) { | ||
busPrototype.reportExecution = function( callpath, routes, duration ) { | ||
if(this.options){ | ||
var self = this; | ||
this._.each( routes, function(element, index, list){ | ||
if( !self.metrics[element.path] ) | ||
self.metrics[element.path] = []; | ||
self.metrics[element.path].push( { version:element.version, duration:duration } ); | ||
routes.forEach(function(element, index, list) { | ||
if( !self.metrics[element.version] ) | ||
self.metrics[element.version] = {}; | ||
if( !self.metrics[element.version][element.path] ) | ||
self.metrics[element.version][element.path] = { maxDuration:0, minDuration:0, sumDuration:0, count:0 }; | ||
var metrics = self.metrics[element.version][element.path]; | ||
metrics.maxDuration = metrics.maxDuration < duration ? duration : metrics.maxDuration; | ||
metrics.minDuration = metrics.minDuration > duration ? duration : metrics.minDuration; | ||
metrics.sumDuration += duration; | ||
metrics.count++; | ||
} ); | ||
@@ -35,4 +55,3 @@ } | ||
if(this.options){ | ||
if(this.logger) | ||
this.logger.debug('Populating monitoring results...'); | ||
this.logger.debug('Populating monitoring results...'); | ||
@@ -42,2 +61,3 @@ var self = this; | ||
if(this.options.newrelic){ | ||
self.newrelic.populate( self.metrics, self.lastPopulation ); | ||
} | ||
@@ -53,3 +73,3 @@ | ||
self.metrics = {}; | ||
self.metrics = { }; | ||
} | ||
@@ -56,0 +76,0 @@ }; |
@@ -6,3 +6,3 @@ /* | ||
*/ | ||
var VERSION = '0.0.41'; | ||
var VERSION = '0.0.42'; | ||
@@ -24,3 +24,3 @@ var connect = require('connect'); | ||
var httphelper = require('./http-helper'); | ||
var bus = new Bus(httphelper, _); | ||
var bus = new Bus( httphelper ); | ||
@@ -119,3 +119,3 @@ var LOAD_SIZE_LIMIT = 1e6; | ||
function(err, results){ | ||
logger.info('Service(s) calling finished.', err, results ); | ||
logger.info( 'Service(s) calling finished.', err, results ); | ||
@@ -129,7 +129,7 @@ var result; | ||
if( asyncCall ){ | ||
httphelper.generalCall( http, https, url, _, asyncCall, 'POST', err, result, logger, function(er, response){ | ||
httphelper.generalCall( asyncCall, 'POST', null, err, result, logger, function(err, result, status){ | ||
if(er) | ||
logger.error( er ); | ||
else | ||
logger.info('Response:', response); | ||
logger.info('Response:', response, status); | ||
} ); | ||
@@ -171,3 +171,3 @@ } else{ | ||
}; | ||
exports.delete = function deleteRest(path, functionRef, prototypeObject, options){ | ||
exports.del = function deleteRest(path, functionRef, prototypeObject, options){ | ||
addPath("DELETE", path, functionRef, prototypeObject, options || {} ); | ||
@@ -212,2 +212,9 @@ }; | ||
exports.httphelper = httphelper; | ||
exports.shutdown = function(){ | ||
bus.shutdown(); | ||
logger.info('Halting connect-rest.'); | ||
}; | ||
exports.rester = function( options ) { | ||
@@ -214,0 +221,0 @@ var domain; |
@@ -0,1 +1,6 @@ | ||
var http = require('http'); | ||
var https = require('https'); | ||
var _ = require('underscore'); | ||
var url = require('url'); | ||
exports.opt = { | ||
@@ -11,3 +16,3 @@ hostname: 'localhost', | ||
}; | ||
exports.generalCall = function(http, https, url, _, serverURL, method, err, result, logger, callback){ | ||
exports.generalCall = function(serverURL, method, headers, err, result, logger, callback){ | ||
var server = url.parse( serverURL ); | ||
@@ -25,4 +30,13 @@ | ||
var lib =(server.protocol == 'https:' ? https : http); | ||
if( headers ){ | ||
for (var name in headers) | ||
if (headers.hasOwnProperty(name)) | ||
voptions.headers[ name ] = headers[ name ]; | ||
} | ||
if(logger) | ||
logger.debug('Options to be used:', voptions); | ||
var lib =(server.protocol === 'https:' ? https : http); | ||
var data; | ||
@@ -32,3 +46,3 @@ var payload = err ? { errorMessage: err.message, errorCode: err.errorCode||err.code||err.statusCode||-1 } : result; | ||
data = JSON.stringify( payload ); | ||
voptions.headers['Content-Length'] = data.length; | ||
//voptions.headers['Content-Length'] = data.length; | ||
if(logger) | ||
@@ -38,2 +52,3 @@ logger.debug('Payload to be sent:', data); | ||
var responseStatus; | ||
var req = lib.request( voptions, function(res) { | ||
@@ -45,7 +60,8 @@ var body = ''; | ||
res.on('end', function ( ) { | ||
callback(null, body); | ||
responseStatus = { statusCode: res.statusCode, headers: res.headers }; | ||
callback(null, (body && res.headers['content-type'] && res.headers['content-type'] === 'application/json') ? JSON.parse(body) : body, responseStatus ); | ||
}); | ||
}); | ||
req.on('error', function(er) { | ||
callback(er, 'failed.'); | ||
callback(er, 'Failed.', responseStatus); | ||
}); | ||
@@ -52,0 +68,0 @@ if( data ) |
@@ -38,3 +38,2 @@ var PARAMETER_M_DELIMETER = ':'; | ||
if( _( ptokens[i] ).startsWith( PARAMETER_O_DELIMETER ) && utokens.length < ptokens.length ){ | ||
//utokens.push(''); | ||
utokens.splice( i, 0, '' ); | ||
@@ -41,0 +40,0 @@ } |
@@ -12,3 +12,3 @@ var Path = require('./path'); | ||
action( request, content, function(err, response){ | ||
self.bus.reportCall( request.headers.originalUrl, self.routes, (Date.now()-time) ); | ||
self.bus.reportExecution( request.headers.originalUrl, self.routes, (Date.now()-time) ); | ||
callback( err, response ); | ||
@@ -15,0 +15,0 @@ } ); |
{ | ||
"name": "connect-rest", | ||
"version": "0.0.41", | ||
"description": "RESTful web services middleware for Connect.", | ||
"keywords": [ | ||
"connect", | ||
"rest", | ||
"restful" | ||
], | ||
"homepage": "https://github.com/imrefazekas/connect-rest", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/imrefazekas/connect-rest.git" | ||
}, | ||
"bugs": { | ||
"url": "http://github.com/imrefazekas/connect-rest/issues" | ||
}, | ||
"author": { | ||
"name": "Imre Fazekas", | ||
"email": "imre.fazekas@gmail.com" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://www.opensource.org/licenses/MIT" | ||
} | ||
], | ||
"main": "./lib/connect-rest.js", | ||
"dependencies": { | ||
"async": ">=0.2.0", | ||
"underscore":"~1", | ||
"underscore.string":"~2", | ||
"semver": "~1", | ||
"bunyan": ">=0.21.0", | ||
"connect": "~2" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-jshint": "~0.2.0", | ||
"grunt-simple-mocha": "~0.3.2", | ||
"should": "~1.2.2", | ||
"async": ">=0.1.22" | ||
}, | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
}, | ||
"readmeFilename": "README.md", | ||
"readme": "README.md", | ||
"_id": "connect-rest@0.0.41", | ||
"_from": "connect-rest@>=0.0.41" | ||
"name": "connect-rest", | ||
"version": "0.0.42", | ||
"description": "RESTful web services middleware for Connect.", | ||
"keywords": [ | ||
"connect", | ||
"rest", | ||
"restful" | ||
], | ||
"homepage": "https://github.com/imrefazekas/connect-rest", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/imrefazekas/connect-rest.git" | ||
}, | ||
"bugs": { | ||
"url": "http://github.com/imrefazekas/connect-rest/issues" | ||
}, | ||
"author": { | ||
"name": "Imre Fazekas", | ||
"email": "imre.fazekas@gmail.com" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://www.opensource.org/licenses/MIT" | ||
} | ||
], | ||
"main": "./lib/connect-rest.js", | ||
"dependencies": { | ||
"async": ">=0.2.0", | ||
"underscore": "~1", | ||
"underscore.string": "~2", | ||
"semver": "~1", | ||
"bunyan": ">=0.21.0", | ||
"connect": "~2" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-jshint": "~0.6.0", | ||
"grunt-contrib-connect": "~0.3.0", | ||
"grunt-contrib-nodeunit": "~0.2.0", | ||
"should": "~1.2.2" | ||
}, | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
}, | ||
"_id": "connect-rest@0.0.42", | ||
"_from": "connect-rest@>=0.0.42" | ||
} |
@@ -427,10 +427,19 @@ [connect-rest](https://github.com/imrefazekas/connect-rest) is a featureful very easy-to-use middleware for [connect](http://www.senchalabs.org/connect/) for building REST APIs. The library has a stunning feature list beyond basic rest functionality. | ||
listener: function(data){ ... } | ||
, newrelic: { | ||
platformApiUri: 'https://platform-api.newrelic.com/platform/v1/metrics', | ||
licenseKey: 'XXX', | ||
pluginName: 'org.vii.connectrest.performancePlugin' | ||
} | ||
} | ||
}; | ||
By adding a monitoring to the options of the library, the monitoring can be activated. The population interval is defined via the _populateInterval_ property measured in millisecs. The property _console_ - if present - will print the commulated execution times grouped/structured by paths and version to the console. | ||
By adding a monitoring to the options of the library, the monitoring can be activated. The population interval is defined via the _populateInterval_ property measured in millisecs. | ||
The property _console_ - if present - will print the commulated execution times grouped/structured by paths and version to the console. | ||
The property _listener_ - if present - allows you to pass a function which the populated data will be sent to. This way you can define own function to process the collected measurements. | ||
The property _newrelic_ - if present - activates the [newrelic](https://newrelic.com) services posting all metrics to the newrelic server. You have to give your license key to make it work properly. | ||
Note: [newrelic](https://newrelic.com) support is under heavy development ... | ||
Note: [newrelic](https://newrelic.com) support is preliminary at this moment. Will be improved by time... | ||
@@ -500,2 +509,3 @@ | ||
- 0.0.42: Incomint request count monitoring added | ||
- 0.0.41: listener for populated measurements can be set | ||
@@ -502,0 +512,0 @@ - 0.0.40: monitoring services (bus) added |
@@ -5,3 +5,2 @@ var rest = require('../../lib/connect-rest'); | ||
var http = require('http'); | ||
var url = require('url'); | ||
var querystring = require("querystring"); | ||
@@ -12,3 +11,2 @@ | ||
var async = require('async'); | ||
var _ = require('underscore'); | ||
@@ -42,3 +40,3 @@ var connectApp = connect(); | ||
console.log('Service URL', serverURL); | ||
httphelper.generalCall( http, null, url, _, serverURL, 'PORT', null, {Message:'Hello'}, null, function(er, response){ | ||
httphelper.generalCall( serverURL, 'POST', null, null, {Message:'Hello'}, null, function(er, response){ | ||
if(er) | ||
@@ -45,0 +43,0 @@ console.error( er ); |
@@ -1,2 +0,2 @@ | ||
function buildUpRestAPI( rest, _ ){ | ||
function buildUpRestAPI( rest ){ | ||
//rest.context( '/api' ); | ||
@@ -8,9 +8,13 @@ | ||
}); | ||
rest.get('/empty', function( request ){ | ||
console.log( 'Received:' + JSON.stringify( request ) ); | ||
return ''; | ||
}); | ||
rest.get('/books/:title/:chapter', function( request ){ | ||
console.log( 'Received:' + JSON.stringify( request ) ); | ||
return 'ok'; | ||
return request.parameters; | ||
}); | ||
rest.post('/store/?id', function( request, content, callback ){ | ||
console.log( 'Received:' + JSON.stringify( request ) + ' ' + JSON.stringify(content) ); | ||
return callback(null, 'ok'); | ||
return callback(null, request.parameters); | ||
}); | ||
@@ -23,3 +27,3 @@ rest.get('/inquire/*book', function( request, content, callback ){ | ||
console.log( 'Received:' + JSON.stringify( request ) + ' ' + JSON.stringify(content) ); | ||
return callback(null, 'ok'); | ||
return callback(null, request.parameters ); | ||
}); | ||
@@ -41,11 +45,11 @@ rest.post( { path: '/make', version: '>=1.0.0' }, function( request, content, callback ){ | ||
console.log( 'Received::' + JSON.stringify( request ) + ' ' + JSON.stringify(content) ); | ||
return callback(null, '', {statusCode:201} ); | ||
}, { contentType:'text/plain', validator: function(req, res){ return true; } } ); | ||
return callback(null, request.parameters, {statusCode:201} ); | ||
}, { contentType:'application/json', validator: function(req, res){ return true; } } ); | ||
rest.get( '/:system/?entity/?version/:subject', function( request, content, callback ){ | ||
rest.get( '/call/:system/?entity/?version/:subject', function( request, content, callback ){ | ||
console.log( 'Received::' + JSON.stringify( request.parameters ) + ' ' + JSON.stringify(content) ); | ||
return callback(null, 'Done.', {statusCode:201} ); | ||
}, { contentType:'text/plain' } ); | ||
return callback(null, request.parameters, {statusCode:201} ); | ||
}, { contentType:'application/json' } ); | ||
} | ||
exports.buildUpRestAPI = buildUpRestAPI; |
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
81857
18
880
538
9