bitcore-node-zcash/insight.js

156 lines
4.2 KiB
JavaScript
Raw Normal View History

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';
//Set the node enviornment variable if not set before
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
2014-08-20 11:40:56 -07:00
var fs = require('fs');
var PeerSync = require('./lib/PeerSync');
var HistoricSync = require('./lib/HistoricSync');
2014-08-21 10:45:32 -07:00
var http = require('http');
var https = require('https');
2014-08-20 11:40:56 -07:00
var express = require('express');
2014-08-20 12:21:05 -07:00
var program = require('commander');
var config = require('./config/config');
2014-08-20 12:21:05 -07:00
var logger = require('./lib/logger').logger;
program
.version(config.version);
2014-01-06 08:33:58 -08:00
2014-07-29 13:28:15 -07:00
// text title
console.log(
'\n\
____ _ __ __ ___ _ \n\
/ _/___ _____(_)___ _/ /_ / /_ / | ____ (_)\n\
/ // __ \\/ ___/ / __ `/ __ \\/ __/ / /\| \| / __ \\/ / \n\
_/ // / / (__ ) / /_/ / / / / /_ / ___ |/ /_/ / / \n\
/___/_/ /_/____/_/\\__, /_/ /_/\\__/ /_/ |_/ .___/_/ \n\
/____/ /_/ \n\
2014-08-20 12:21:05 -07:00
\n\t\t\t\t\t\tv%s\n', config.version);
program.on('--help', function() {
logger.info('\n# Configuration:\n\
2014-07-30 12:23:04 -07:00
\tINSIGHT_NETWORK (Network): %s\n\
\tINSIGHT_DB (Database Path): %s\n\
\tINSIGHT_SAFE_CONFIRMATIONS (Safe Confirmations): %s\n\
\tINSIGHT_IGNORE_CACHE (Ignore Cache): %s\n\
2014-07-29 13:28:15 -07:00
# Bicoind Connection configuration:\n\
2014-07-30 12:23:04 -07:00
\tRPC Username: %s\t\tBITCOIND_USER\n\
\tRPC Password: %s\tBITCOIND_PASS\n\
\tRPC Protocol: %s\t\tBITCOIND_PROTO\n\
\tRPC Host: %s\t\tBITCOIND_HOST\n\
\tRPC Port: %s\t\t\tBITCOIND_PORT\n\
\tP2P Port: %s\t\t\tBITCOIND_P2P_PORT\n\
\tBITCOIND_DATADIR: %s\n\
\t%s\n\
\nChange setting by assigning the enviroment variables above. Example:\n\
2014-07-29 13:28:15 -07:00
$ INSIGHT_NETWORK="testnet" BITCOIND_HOST="123.123.123.123" ./insight.js\
\n\n',
2014-08-20 12:21:05 -07:00
config.network, config.leveldb, config.safeConfirmations, config.ignoreCache ? 'yes' : 'no',
config.bitcoind.user,
config.bitcoind.pass ? 'Yes(hidden)' : 'No',
config.bitcoind.protocol,
config.bitcoind.host,
config.bitcoind.port,
config.bitcoind.p2pPort,
config.bitcoind.dataDir + (config.network === 'testnet' ? '*' : ''), (config.network === 'testnet' ? '* (/testnet3 is added automatically)' : '')
);
});
2014-08-20 11:40:56 -07:00
2014-08-20 12:21:05 -07:00
program.parse(process.argv);
2014-08-20 11:40:56 -07:00
// create express app
2014-01-31 09:20:56 -08:00
var expressApp = express();
2014-08-21 13:36:52 -07:00
// setup headers
require('./config/headers')(expressApp);
2014-08-21 10:45:32 -07:00
// setup http/https base server
2014-08-21 10:47:51 -07:00
var server;
2014-08-21 10:45:32 -07:00
if (config.enableHTTPS) {
2014-08-21 10:47:51 -07:00
var serverOpts = {};
2014-08-21 10:45:32 -07:00
serverOpts.key = fs.readFileSync('./etc/test-key.pem');
serverOpts.cert = fs.readFileSync('./etc/test-cert.pem');
2014-08-21 10:47:51 -07:00
server = https.createServer(serverOpts, expressApp);
} else {
server = http.createServer(expressApp);
2014-08-21 10:45:32 -07:00
}
2014-08-20 11:40:56 -07:00
// 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-08-21 14:34:15 -07:00
// p2pSync process
2014-07-29 13:28:15 -07:00
var peerSync = new PeerSync({
shouldBroadcast: true
});
2014-02-08 15:09:54 -08:00
if (!config.disableP2pSync) {
peerSync.run();
2014-02-08 15:09:54 -08:00
}
2014-08-21 14:34:15 -07:00
// historic_sync process
var historicSync = new HistoricSync({
shouldBroadcastSync: true
});
peerSync.historicSync = historicSync;
2014-01-21 12:46:42 -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);
}
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-06 08:33:58 -08:00
2014-01-13 13:13:41 -08:00
// socket.io
2014-08-20 09:21:58 -07:00
var ios = require('socket.io')(server, config);
2014-08-20 09:05:27 -07:00
require('./app/controllers/socket.js').init(ios);
2014-08-20 09:21:58 -07:00
// plugins
2014-08-20 14:48:23 -07:00
if (config.enableRatelimiter) {
require('./plugins/ratelimiter').init(expressApp, config.ratelimiter);
}
2014-08-20 09:21:58 -07:00
if (config.enableMailbox) {
require('./plugins/mailbox').init(ios, config.mailbox);
2014-08-20 09:05:27 -07:00
}
2014-01-13 13:13:41 -08:00
2014-09-01 07:36:25 -07:00
if (config.enableCleaner) {
require('./plugins/cleaner').init(config.cleaner);
}
2014-08-20 11:40:56 -07:00
2014-08-20 12:37:25 -07:00
// express settings
require('./config/express')(expressApp, historicSync, peerSync);
2014-08-21 14:34:15 -07:00
require('./config/routes')(expressApp);
2014-08-20 12:37:25 -07: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() {
2014-08-20 12:37:25 -07:00
logger.info('insight server listening on port %d in %s mode', server.address().port, process.env.NODE_ENV);
});
2014-01-06 08:33:58 -08:00
//expose app
2014-01-31 09:20:56 -08:00
exports = module.exports = expressApp;