insight-ui-zcash/insight.js

101 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-01-06 08:33:58 -08:00
'use strict';
//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').class(),
HistoricSync = require('./lib/HistoricSync').class();
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-01-31 09:20:56 -08:00
/**
* historic_sync process
*/
2014-01-19 05:09:59 -08:00
var historicSync = {};
2014-01-21 12:46:42 -08:00
if (!config.disableHistoricSync) {
2014-01-19 05:09:59 -08:00
historicSync = new HistoricSync();
2014-01-21 12:46:42 -08:00
2014-01-19 05:09:59 -08:00
historicSync.init({
shouldBroadcast: true,
networkName: config.network
2014-01-21 11:43:05 -08:00
}, function(err) {
if (err) {
var txt = 'ABORTED with error: ' + err.message;
console.log('[historic_sync] ' + txt);
}
else {
2014-02-05 08:38:39 -08:00
historicSync.smartImport({}, function(err){
2014-01-31 09:20:56 -08:00
var txt = 'ended.';
2014-01-21 11:43:05 -08:00
if (err) txt = 'ABORTED with error: ' + err.message;
2014-01-21 14:13:21 -08:00
console.log('[historic_sync] ' + txt, historicSync.info());
2014-01-21 11:43:05 -08:00
});
}
2014-01-16 11:11:55 -08:00
});
}
2014-01-16 11:11:55 -08:00
2014-01-31 09:20:56 -08:00
/**
* p2pSync process
*/
if (!config.disableP2pSync) {
var ps = new PeerSync();
ps.init({
broadcast_txs: true,
2014-01-29 10:10:36 -08:00
broadcast_address_tx: true,
broadcast_blocks: true,
}, function() {
ps.run();
});
}
2014-01-06 08:33:58 -08:00
//express settings
2014-01-31 09:20:56 -08:00
require('./config/express')(expressApp, historicSync);
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(){
console.log('Express 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;