2014-05-26 09:29:52 -07:00
|
|
|
#!/usr/bin/env node
|
2014-02-20 05:37:34 -08:00
|
|
|
|
2014-05-26 09:29:52 -07:00
|
|
|
'use strict';
|
2014-01-13 10:17:34 -08:00
|
|
|
//Set the node enviornment variable if not set before
|
|
|
|
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
|
|
|
|
2014-01-06 08:33:58 -08:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
var express = require('express'),
|
2014-01-13 10:17:34 -08:00
|
|
|
fs = require('fs'),
|
2014-03-10 08:46:48 -07:00
|
|
|
PeerSync = require('./lib/PeerSync'),
|
|
|
|
HistoricSync = require('./lib/HistoricSync');
|
2014-01-16 18:23:10 -08:00
|
|
|
|
2014-01-06 09:37:32 -08:00
|
|
|
//Initializing system variables
|
2014-01-13 10:17:34 -08:00
|
|
|
var config = require('./config/config');
|
2014-01-06 08:33:58 -08:00
|
|
|
|
2014-07-29 13:28:15 -07:00
|
|
|
// text title
|
|
|
|
/*jshint multistr: true */
|
|
|
|
console.log(
|
|
|
|
'\n\
|
|
|
|
____ _ __ __ ___ _ \n\
|
|
|
|
/ _/___ _____(_)___ _/ /_ / /_ / | ____ (_)\n\
|
|
|
|
/ // __ \\/ ___/ / __ `/ __ \\/ __/ / /\| \| / __ \\/ / \n\
|
|
|
|
_/ // / / (__ ) / /_/ / / / / /_ / ___ |/ /_/ / / \n\
|
|
|
|
/___/_/ /_/____/_/\\__, /_/ /_/\\__/ /_/ |_/ .___/_/ \n\
|
|
|
|
/____/ /_/ \n\
|
|
|
|
\n\t\t\t\t\t\tv%s\n\
|
|
|
|
# Configuration:\n\
|
|
|
|
\t\tNetwork: %s\tINSIGHT_NETWORK\n\
|
|
|
|
\t\tDatabase Path: %s\tINSIGHT_DB\n\
|
|
|
|
\t\tSafe Confirmations: %s\tINSIGHT_SAFE_CONFIRMATIONS\n\
|
|
|
|
\t\tIgnore Cache: %s\tINSIGHT_IGNORE_CACHE\n\
|
|
|
|
# Bicoind Connection configuration:\n\
|
|
|
|
\t\tRPC Username: %s\tBITCOIND_USER\n\
|
|
|
|
\t\tRPC Password: %s\tBITCOIND_PASS\n\
|
|
|
|
\t\tRPC Protocol: %s\tBITCOIND_PROTO\n\
|
|
|
|
\t\tRPC Host: %s\tBITCOIND_HOST\n\
|
|
|
|
\t\tRPC Port: %s\tBITCOIND_PORT\n\
|
|
|
|
\t\tP2P Port: %s\tBITCOIND_P2P_PORT\n\
|
|
|
|
\t\tData Dir: %s\tBITCOIND_DATADIR\n\
|
|
|
|
\t\t%s\n\
|
|
|
|
\nChange setting by assigning the enviroment variables in the last column. Example:\n\
|
|
|
|
$ INSIGHT_NETWORK="testnet" BITCOIND_HOST="123.123.123.123" ./insight.js\
|
|
|
|
\n\n',
|
|
|
|
version,
|
|
|
|
network, home, safeConfirmations, ignoreCache ? 'yes' : 'no',
|
|
|
|
bitcoindConf.user,
|
|
|
|
bitcoindConf.pass ? 'Yes(hidden)' : 'No',
|
|
|
|
bitcoindConf.protocol,
|
|
|
|
bitcoindConf.host,
|
|
|
|
bitcoindConf.port,
|
|
|
|
bitcoindConf.p2pPort,
|
|
|
|
dataDir + (network === 'testnet' ? '*' : ''), (network === 'testnet' ? '* (/testnet3 is added automatically)' : '')
|
|
|
|
);
|
|
|
|
|
2014-01-31 09:20:56 -08:00
|
|
|
/**
|
|
|
|
* express app
|
|
|
|
*/
|
|
|
|
var expressApp = express();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bootstrap models
|
|
|
|
*/
|
2014-01-06 08:33:58 -08:00
|
|
|
var models_path = __dirname + '/app/models';
|
|
|
|
var walk = function(path) {
|
2014-01-06 09:37:32 -08:00
|
|
|
fs.readdirSync(path).forEach(function(file) {
|
|
|
|
var newPath = path + '/' + file;
|
|
|
|
var stat = fs.statSync(newPath);
|
|
|
|
if (stat.isFile()) {
|
2014-01-31 09:20:56 -08:00
|
|
|
if (/(.*)\.(js$)/.test(file)) {
|
2014-01-06 09:37:32 -08:00
|
|
|
require(newPath);
|
|
|
|
}
|
2014-07-29 13:28:15 -07:00
|
|
|
} else if (stat.isDirectory()) {
|
2014-01-06 09:37:32 -08:00
|
|
|
walk(newPath);
|
|
|
|
}
|
|
|
|
});
|
2014-01-06 08:33:58 -08:00
|
|
|
};
|
2014-01-31 09:20:56 -08:00
|
|
|
|
2014-01-06 08:33:58 -08:00
|
|
|
walk(models_path);
|
|
|
|
|
2014-02-08 15:09:54 -08:00
|
|
|
/**
|
|
|
|
* p2pSync process
|
|
|
|
*/
|
2014-02-12 11:27:18 -08:00
|
|
|
|
2014-07-29 13:28:15 -07:00
|
|
|
var peerSync = new PeerSync({
|
|
|
|
shouldBroadcast: true
|
|
|
|
});
|
2014-02-12 11:27:18 -08:00
|
|
|
|
2014-02-08 15:09:54 -08:00
|
|
|
if (!config.disableP2pSync) {
|
2014-02-17 22:40:55 -08:00
|
|
|
peerSync.run();
|
2014-02-08 15:09:54 -08:00
|
|
|
}
|
|
|
|
|
2014-01-31 09:20:56 -08:00
|
|
|
/**
|
|
|
|
* historic_sync process
|
|
|
|
*/
|
2014-02-18 09:01:18 -08:00
|
|
|
var historicSync = new HistoricSync({
|
|
|
|
shouldBroadcastSync: true
|
|
|
|
});
|
2014-02-17 22:40:55 -08:00
|
|
|
peerSync.historicSync = historicSync;
|
2014-01-21 12:46:42 -08:00
|
|
|
|
2014-01-16 18:23:10 -08:00
|
|
|
if (!config.disableHistoricSync) {
|
2014-07-29 13:28:15 -07:00
|
|
|
historicSync.start({}, function(err) {
|
2014-01-21 11:43:05 -08:00
|
|
|
if (err) {
|
|
|
|
var txt = 'ABORTED with error: ' + err.message;
|
|
|
|
console.log('[historic_sync] ' + txt);
|
|
|
|
}
|
2014-02-17 22:40:55 -08:00
|
|
|
if (peerSync) peerSync.allowReorgs = true;
|
2014-01-16 11:11:55 -08:00
|
|
|
});
|
2014-07-29 13:28:15 -07:00
|
|
|
} else
|
|
|
|
if (peerSync) peerSync.allowReorgs = true;
|
2014-02-08 15:09:54 -08:00
|
|
|
|
2014-01-13 10:17:34 -08:00
|
|
|
|
2014-01-06 08:33:58 -08:00
|
|
|
//express settings
|
2014-02-12 11:27:18 -08:00
|
|
|
require('./config/express')(expressApp, historicSync, peerSync);
|
2014-01-06 08:33:58 -08:00
|
|
|
|
|
|
|
//Bootstrap routes
|
2014-01-31 09:20:56 -08:00
|
|
|
require('./config/routes')(expressApp);
|
2014-01-06 08:33:58 -08:00
|
|
|
|
2014-01-13 13:13:41 -08:00
|
|
|
// socket.io
|
2014-01-31 09:20:56 -08:00
|
|
|
var server = require('http').createServer(expressApp);
|
2014-06-04 12:45:33 -07:00
|
|
|
var ios = require('socket.io')(server);
|
2014-01-31 09:20:56 -08:00
|
|
|
require('./app/controllers/socket.js').init(expressApp, ios);
|
2014-01-13 13:13:41 -08:00
|
|
|
|
2014-01-06 08:33:58 -08:00
|
|
|
//Start the app by listening on <port>
|
2014-07-29 13:28:15 -07:00
|
|
|
server.listen(config.port, function() {
|
|
|
|
console.log('insight server listening on port %d in %s mode', server.address().port, process.env.NODE_ENV);
|
2014-01-14 10:20:59 -08:00
|
|
|
});
|
2014-01-06 08:33:58 -08:00
|
|
|
|
|
|
|
//expose app
|
2014-01-31 09:20:56 -08:00
|
|
|
exports = module.exports = expressApp;
|