use winston for logging
This commit is contained in:
parent
8d672de5bd
commit
a5fda98cdc
@ -88,7 +88,7 @@ INSIGHT_NETWORK [= 'livenet' | 'testnet']
|
|||||||
INSIGHT_DB # Path where to store insight's internal DB. (defaults to $HOME/.insight)
|
INSIGHT_DB # Path where to store insight's internal DB. (defaults to $HOME/.insight)
|
||||||
INSIGHT_SAFE_CONFIRMATIONS=6 # Nr. of confirmation needed to start caching transaction information
|
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.
|
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_MESSAGE_BROKER # if "true" will enable message broker module
|
ENABLE_MAILBOX # if "true" will enable message broker module
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -3,24 +3,17 @@
|
|||||||
// server-side socket behaviour
|
// server-side socket behaviour
|
||||||
var ios = null; // io is already taken in express
|
var ios = null; // io is already taken in express
|
||||||
var util = require('bitcore').util;
|
var util = require('bitcore').util;
|
||||||
|
var logger = require('../../lib/logger');
|
||||||
|
|
||||||
var verbose = false;
|
module.exports.init = function(io_ext) {
|
||||||
var log = function() {
|
|
||||||
if (verbose) {
|
|
||||||
console.log(arguments);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.init = function(io_ext, config) {
|
|
||||||
ios = io_ext;
|
ios = io_ext;
|
||||||
verbose = config.verbose;
|
|
||||||
if (ios) {
|
if (ios) {
|
||||||
// when a new socket connects
|
// when a new socket connects
|
||||||
ios.sockets.on('connection', function(socket) {
|
ios.sockets.on('connection', function(socket) {
|
||||||
log('New connection from ' + socket.id);
|
logger.info('New connection from ' + socket.id);
|
||||||
// when it subscribes, make it join the according room
|
// when it subscribes, make it join the according room
|
||||||
socket.on('subscribe', function(topic) {
|
socket.on('subscribe', function(topic) {
|
||||||
log('subscribe to ' + topic);
|
logger.info('subscribe to ' + topic);
|
||||||
socket.join(topic);
|
socket.join(topic);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -76,7 +69,7 @@ module.exports.broadcastSyncInfo = function(historicSync) {
|
|||||||
var broadcastMessage = module.exports.broadcastMessage = function(message, socket) {
|
var broadcastMessage = module.exports.broadcastMessage = function(message, socket) {
|
||||||
if (ios) {
|
if (ios) {
|
||||||
var s = socket || ios.sockets.in(message.to);
|
var s = socket || ios.sockets.in(message.to);
|
||||||
log('sending message to ' + message.to);
|
logger.info('sending message to ' + message.to);
|
||||||
s.emit('message', message);
|
s.emit('message', message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -61,7 +61,6 @@ dataDir += network === 'testnet' ? 'testnet3' : '';
|
|||||||
|
|
||||||
var safeConfirmations = process.env.INSIGHT_SAFE_CONFIRMATIONS || 6;
|
var safeConfirmations = process.env.INSIGHT_SAFE_CONFIRMATIONS || 6;
|
||||||
var ignoreCache = process.env.INSIGHT_IGNORE_CACHE || 0;
|
var ignoreCache = process.env.INSIGHT_IGNORE_CACHE || 0;
|
||||||
var verbose = process.env.VERBOSE === 'true';
|
|
||||||
|
|
||||||
|
|
||||||
var bitcoindConf = {
|
var bitcoindConf = {
|
||||||
@ -91,7 +90,6 @@ if (!fs.existsSync(db)) {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
enableMailbox: enableMailbox,
|
enableMailbox: enableMailbox,
|
||||||
verbose: verbose,
|
|
||||||
version: version,
|
version: version,
|
||||||
root: rootPath,
|
root: rootPath,
|
||||||
publicPath: process.env.INSIGHT_PUBLIC_PATH || false,
|
publicPath: process.env.INSIGHT_PUBLIC_PATH || false,
|
||||||
|
|||||||
50
insight.js
50
insight.js
@ -119,54 +119,12 @@ require('./config/routes')(expressApp);
|
|||||||
|
|
||||||
// socket.io
|
// socket.io
|
||||||
var server = require('http').createServer(expressApp);
|
var server = require('http').createServer(expressApp);
|
||||||
var ios = require('socket.io')(server);
|
var ios = require('socket.io')(server, config);
|
||||||
|
require('./app/controllers/socket.js').init(ios);
|
||||||
|
|
||||||
// plugins
|
// plugins
|
||||||
require('./app/controllers/socket.js').init(ios);
|
if (config.enableMailbox) {
|
||||||
if (config.enableMessageBroker) {
|
require('./plugins/mailbox').init(ios, config.mailbox);
|
||||||
var mdb = require('../../lib/MessageDb').default();
|
|
||||||
|
|
||||||
// when it requests sync, send him all pending messages
|
|
||||||
socket.on('sync', function(ts) {
|
|
||||||
log('Sync requested by ' + socket.id);
|
|
||||||
log(' from timestamp ' + ts);
|
|
||||||
var rooms = socket.rooms;
|
|
||||||
if (rooms.length !== 2) {
|
|
||||||
socket.emit('insight-error', 'Must subscribe with public key before syncing');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var to = rooms[1];
|
|
||||||
var upper_ts = Math.round(microtime.now());
|
|
||||||
log(' to timestamp ' + upper_ts);
|
|
||||||
mdb.getMessages(to, ts, upper_ts, function(err, messages) {
|
|
||||||
if (err) {
|
|
||||||
throw new Error('Couldn\'t get messages on sync request: ' + err);
|
|
||||||
}
|
|
||||||
log('\tFound ' + messages.length + ' message' + (messages.length !== 1 ? 's' : ''));
|
|
||||||
for (var i = 0; i < messages.length; i++) {
|
|
||||||
broadcastMessage(messages[i], socket);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// when it sends a message, add it to db
|
|
||||||
socket.on('message', function(m) {
|
|
||||||
log('Message sent from ' + m.pubkey + ' to ' + m.to);
|
|
||||||
mdb.addMessage(m, function(err) {
|
|
||||||
if (err) {
|
|
||||||
throw new Error('Couldn\'t add message to database: ' + err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// disconnect handler
|
|
||||||
socket.on('disconnect', function() {
|
|
||||||
log('disconnected ' + socket.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
mdb.on('message', broadcastMessage);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Start the app by listening on <port>
|
//Start the app by listening on <port>
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
var winston = require('winston');
|
var winston = require('winston');
|
||||||
|
|
||||||
|
|
||||||
|
winston.transports.console.level = 'debug';
|
||||||
winston.info('starting...')
|
winston.info('starting...')
|
||||||
|
|
||||||
module.exports.logger=winston;
|
module.exports.logger=winston;
|
||||||
|
|||||||
@ -1,2 +1,90 @@
|
|||||||
|
|
||||||
var microtime = require('microtime');
|
var microtime = require('microtime');
|
||||||
|
var mdb = require('../../lib/MessageDb').default();
|
||||||
|
|
||||||
|
|
||||||
|
module.exports.init = function(ios, config) {
|
||||||
|
|
||||||
|
ios.sockets.on('connection', function(socket) {
|
||||||
|
// when it requests sync, send him all pending messages
|
||||||
|
socket.on('sync', function(ts) {
|
||||||
|
log('Sync requested by ' + socket.id);
|
||||||
|
log(' from timestamp ' + ts);
|
||||||
|
var rooms = socket.rooms;
|
||||||
|
if (rooms.length !== 2) {
|
||||||
|
socket.emit('insight-error', 'Must subscribe with public key before syncing');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var to = rooms[1];
|
||||||
|
var upper_ts = Math.round(microtime.now());
|
||||||
|
log(' to timestamp ' + upper_ts);
|
||||||
|
mdb.getMessages(to, ts, upper_ts, function(err, messages) {
|
||||||
|
if (err) {
|
||||||
|
throw new Error('Couldn\'t get messages on sync request: ' + err);
|
||||||
|
}
|
||||||
|
log('\tFound ' + messages.length + ' message' + (messages.length !== 1 ? 's' : ''));
|
||||||
|
for (var i = 0; i < messages.length; i++) {
|
||||||
|
broadcastMessage(messages[i], socket);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// when it sends a message, add it to db
|
||||||
|
socket.on('message', function(m) {
|
||||||
|
log('Message sent from ' + m.pubkey + ' to ' + m.to);
|
||||||
|
mdb.addMessage(m, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw new Error('Couldn\'t add message to database: ' + err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// disconnect handler
|
||||||
|
socket.on('disconnect', function() {
|
||||||
|
log('disconnected ' + socket.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
mdb.on('message', broadcastMessage);
|
||||||
|
// when it requests sync, send him all pending messages
|
||||||
|
socket.on('sync', function(ts) {
|
||||||
|
log('Sync requested by ' + socket.id);
|
||||||
|
log(' from timestamp ' + ts);
|
||||||
|
var rooms = socket.rooms;
|
||||||
|
if (rooms.length !== 2) {
|
||||||
|
socket.emit('insight-error', 'Must subscribe with public key before syncing');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var to = rooms[1];
|
||||||
|
var upper_ts = Math.round(microtime.now());
|
||||||
|
log(' to timestamp ' + upper_ts);
|
||||||
|
mdb.getMessages(to, ts, upper_ts, function(err, messages) {
|
||||||
|
if (err) {
|
||||||
|
throw new Error('Couldn\'t get messages on sync request: ' + err);
|
||||||
|
}
|
||||||
|
log('\tFound ' + messages.length + ' message' + (messages.length !== 1 ? 's' : ''));
|
||||||
|
for (var i = 0; i < messages.length; i++) {
|
||||||
|
broadcastMessage(messages[i], socket);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// when it sends a message, add it to db
|
||||||
|
socket.on('message', function(m) {
|
||||||
|
log('Message sent from ' + m.pubkey + ' to ' + m.to);
|
||||||
|
mdb.addMessage(m, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw new Error('Couldn\'t add message to database: ' + err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// disconnect handler
|
||||||
|
socket.on('disconnect', function() {
|
||||||
|
log('disconnected ' + socket.id);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
mdb.on('message', broadcastMessage);
|
||||||
|
|
||||||
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user