insight-ui-zcash/insight.js

120 lines
2.6 KiB
JavaScript
Raw Normal View History

2014-01-06 08:33:58 -08:00
'use strict';
//Load configurations
//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(),
2014-01-16 11:11:55 -08:00
HistoricSync = require('./lib/HistoricSync').class(),
mongoose = require('mongoose');
2014-01-06 08:33:58 -08:00
2014-01-06 08:33:58 -08:00
/**
* Main application entry file.
*/
2014-01-06 09:37:32 -08:00
//Initializing system variables
var config = require('./config/config');
2014-01-06 08:33:58 -08:00
//Bootstrap db connection
// If mongod is running
2014-01-21 06:28:30 -08:00
mongoose.connection.on('open', function () {
console.log('Connected to mongo server.');
});
// If mongod is not running
mongoose.connection.on('error', function (err) {
console.log('Could not connect to mongo server!');
console.log(err);
});
2014-01-19 05:09:59 -08:00
mongoose.connect(config.db);
2014-01-06 08:33:58 -08:00
//Bootstrap models
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()) {
if (/(.*)\.(js$|coffee$)/.test(file)) {
require(newPath);
}
} else if (stat.isDirectory()) {
walk(newPath);
}
});
2014-01-06 08:33:58 -08:00
};
walk(models_path);
2014-01-16 11:11:55 -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({
skipDbConnection: true,
shouldBroadcast: true,
2014-01-21 08:28:01 -08:00
progressStep: 2,
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-01-21 14:13:21 -08:00
historicSync.smartImport(function(err){
2014-01-21 11:43:05 -08:00
var txt= 'ended.';
if (err) txt = 'ABORTED with error: ' + err.message;
2014-01-18 13:28:24 -08:00
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
// p2p_sync process
if (!config.disableP2pSync) {
var ps = new PeerSync();
ps.init({
skipDbConnection: true,
broadcast_txs: true,
broadcast_blocks: true
}, function() {
ps.run();
});
}
// express app
2014-01-16 18:26:12 -08:00
/*global app: true*/
var app = express();
2014-01-06 08:33:58 -08:00
//express settings
2014-01-19 05:09:59 -08:00
require('./config/express')(app, historicSync);
2014-01-06 08:33:58 -08:00
//Bootstrap routes
require('./config/routes')(app);
2014-01-13 13:13:41 -08:00
// socket.io
var server = require('http').createServer(app);
var ios = require('socket.io').listen(server);
2014-01-16 18:26:12 -08:00
require('./app/controllers/socket.js').init(app,ios);
2014-01-13 13:13:41 -08:00
2014-01-06 08:33:58 -08:00
//Start the app by listening on <port>
var port = process.env.PORT || config.port;
server.listen(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
exports = module.exports = app;