bitcore-node-zcash/insight.js

93 lines
2.0 KiB
JavaScript
Raw Normal View History

2014-01-06 08:33:58 -08:00
'use strict';
2014-02-20 05:37: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'),
fs = require('fs'),
PeerSync = require('./lib/PeerSync'),
HistoricSync = require('./lib/HistoricSync');
2014-01-06 09:37:32 -08:00
//Initializing system variables
var config = require('./config/config');
2014-01-06 08:33:58 -08:00
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-01-31 09:20:56 -08: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
*/
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-01-31 09:20:56 -08:00
/**
* historic_sync process
*/
var historicSync = new HistoricSync({
shouldBroadcastSync: true
});
peerSync.historicSync = historicSync;
2014-01-21 12:46:42 -08:00
if (!config.disableHistoricSync) {
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
});
}
else
if (peerSync) peerSync.allowReorgs = true;
2014-02-08 15:09:54 -08:00
2014-01-06 08:33:58 -08:00
//express settings
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);
var ios = require('socket.io').listen(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-01-31 09:20:56 -08:00
server.listen(config.port, function(){
2014-02-26 06:50:09 -08:00
console.log('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;