2014-01-06 08:33:58 -08: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'),
|
|
|
|
PeerSync = require('./lib/PeerSync').class(),
|
2014-01-16 11:11:55 -08:00
|
|
|
HistoricSync = require('./lib/HistoricSync').class(),
|
2014-01-13 10:17:34 -08:00
|
|
|
mongoose = require('mongoose');
|
2014-01-06 08:33:58 -08:00
|
|
|
|
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-01-31 09:20:56 -08:00
|
|
|
/**
|
|
|
|
* express app
|
|
|
|
*/
|
|
|
|
var expressApp = express();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bootstrap db connection
|
|
|
|
*/
|
2014-01-21 05:09:51 -08:00
|
|
|
// If mongod is running
|
2014-01-31 09:20:56 -08:00
|
|
|
mongoose.connection.on('open', function() {
|
2014-01-21 05:09:51 -08:00
|
|
|
console.log('Connected to mongo server.');
|
|
|
|
});
|
2014-01-31 09:20:56 -08:00
|
|
|
|
2014-01-21 05:09:51 -08:00
|
|
|
// If mongod is not running
|
2014-01-31 09:20:56 -08:00
|
|
|
mongoose.connection.on('error', function(err) {
|
2014-01-21 05:09:51 -08:00
|
|
|
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
|
|
|
|
2014-01-31 09:20:56 -08: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-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
|
|
|
|
2014-01-16 18:23:10 -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({
|
2014-01-19 07:33:39 -08:00
|
|
|
skipDbConnection: true,
|
|
|
|
shouldBroadcast: true,
|
2014-01-16 18:23:10 -08:00
|
|
|
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-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 18:23:10 -08:00
|
|
|
}
|
2014-01-16 11:11:55 -08:00
|
|
|
|
2014-01-31 09:20:56 -08:00
|
|
|
/**
|
|
|
|
* p2pSync process
|
|
|
|
*/
|
2014-01-16 18:23:10 -08:00
|
|
|
if (!config.disableP2pSync) {
|
|
|
|
var ps = new PeerSync();
|
|
|
|
ps.init({
|
2014-01-19 07:33:39 -08:00
|
|
|
skipDbConnection: true,
|
2014-01-16 18:23:10 -08:00
|
|
|
broadcast_txs: true,
|
2014-01-29 10:10:36 -08:00
|
|
|
broadcast_address_tx: true,
|
|
|
|
broadcast_blocks: true,
|
2014-01-16 18:23:10 -08:00
|
|
|
}, function() {
|
|
|
|
ps.run();
|
|
|
|
});
|
|
|
|
}
|
2014-01-13 10:17:34 -08:00
|
|
|
|
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);
|
2014-01-16 18:23:10 -08:00
|
|
|
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-01-14 10:20:59 -08:00
|
|
|
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;
|