create ratelimiter plugin

This commit is contained in:
Manuel Araoz 2014-08-20 14:40:56 -04:00
parent 0ff490dbf3
commit 461fda09be
6 changed files with 30 additions and 14 deletions

View File

@ -89,6 +89,7 @@ INSIGHT_DB # Path where to store insight's internal DB. (defaults to
INSIGHT_SAFE_CONFIRMATIONS=6 # Nr. of confirmation needed to start caching transaction information
INSIGHT_IGNORE_CACHE # True to ignore cache of spents in transaction, with more than INSIGHT_SAFE_CONFIRMATIONS confirmations. This is useful for tracking double spents for old transactions.
ENABLE_MAILBOX # if "true" will enable mailbox plugin
ENABLE_RATELIMITER # if "true" will enable the ratelimiter plugin
LOGGER_LEVEL # defaults to 'info', can be 'debug','verbose','error', etc.
```

View File

@ -77,6 +77,7 @@ var bitcoindConf = {
};
var enableMailbox = process.env.ENABLE_MAILBOX === 'true';
var enableRatelimiter = process.env.ENABLE_RATELIMITER === 'true';
var loggerLevel = process.env.LOGGER_LEVEL || 'info';
if (!fs.existsSync(db)) {
@ -91,6 +92,7 @@ if (!fs.existsSync(db)) {
module.exports = {
enableMailbox: enableMailbox,
enableRatelimiter: enableRatelimiter,
loggerLevel: loggerLevel,
version: version,
root: rootPath,

View File

@ -4,15 +4,13 @@
//Set the node enviornment variable if not set before
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
/**
* Module dependencies.
*/
var express = require('express'),
fs = require('fs'),
PeerSync = require('./lib/PeerSync'),
HistoricSync = require('./lib/HistoricSync');
var fs = require('fs');
var PeerSync = require('./lib/PeerSync');
var HistoricSync = require('./lib/HistoricSync');
var express = require('express');
var connect = require('connect');
//Initializing system variables
var config = require('./config/config');
// text title
@ -54,14 +52,12 @@ console.log(
config.bitcoind.dataDir + (config.network === 'testnet' ? '*' : ''), (config.network === 'testnet' ? '* (/testnet3 is added automatically)' : '')
);
/**
* express app
*/
// create express app
var expressApp = express();
/**
* Bootstrap models
*/
// Bootstrap models
var models_path = __dirname + '/app/models';
var walk = function(path) {
fs.readdirSync(path).forEach(function(file) {
@ -127,6 +123,10 @@ if (config.enableMailbox) {
require('./plugins/mailbox').init(ios, config.mailbox);
}
if (config.enableRatelimiter) {
require('./plugins/ratelimiter').init(expressApp, config.ratelimiter);
}
//Start the app by listening on <port>
server.listen(config.port, function() {
console.log('insight server listening on port %d in %s mode', server.address().port, process.env.NODE_ENV);

View File

@ -58,6 +58,8 @@
"bufferput": "git://github.com/bitpay/node-bufferput.git",
"buffertools": "*",
"commander": "*",
"connect": "^2.25.7",
"connect-ratelimit": "0.0.6",
"express": "~3.4.7",
"glob": "*",
"leveldown": "*",

View File

@ -5,6 +5,7 @@ var preconditions = require('preconditions').singleton();
var io;
module.exports.init = function(ext_io, config) {
logger.info('Using mailbox plugin');
preconditions.checkArgument(ext_io);
io = ext_io;
io.sockets.on('connection', function(socket) {

10
plugins/ratelimiter.js Normal file
View File

@ -0,0 +1,10 @@
var logger = require('../lib/logger').logger;
var preconditions = require('preconditions').singleton();
var limiter = require('connect-ratelimit');
module.exports.init = function(app, config) {
preconditions.checkArgument(app);
logger.info('Using ratelimiter plugin');
};