diff --git a/.gitignore b/.gitignore index 888b5ce..aabec27 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ node_modules # extras *.swp +*.swo *~ .project peerdb.json diff --git a/app/controllers/status.js b/app/controllers/status.js index eb2f1ae..d1cd93d 100644 --- a/app/controllers/status.js +++ b/app/controllers/status.js @@ -48,10 +48,14 @@ exports.show = function(req, res, next) { res.jsonp(d); }); } - else { res.status(400).send('Bad Request'); } } }; +exports.sync = function(req, res, next) { + if (req.syncInfo) + res.jsonp(req.syncInfo); + next(); +}; diff --git a/config/express.js b/config/express.js index a25db3b..0e5c58d 100644 --- a/config/express.js +++ b/config/express.js @@ -7,7 +7,8 @@ var express = require('express'), helpers = require('view-helpers'), config = require('./config'); -module.exports = function(app, passport, db) { +module.exports = function(app, historicSync) { + app.set('showStackError', true); //Prettify HTML @@ -26,9 +27,17 @@ module.exports = function(app, passport, db) { app.set('view engine', 'jade'); //Enable jsonp - app.enable("jsonp callback"); + app.enable('jsonp callback'); + //custom middleware + function setHistoric(req, res, next) { + req.syncInfo = historicSync.syncInfo; + next(); + } + app.use('/api/sync', setHistoric); + app.configure(function() { + //cookieParser should be above session app.use(express.cookieParser()); @@ -43,6 +52,7 @@ module.exports = function(app, passport, db) { //routes should be at the last app.use(app.router); + //Setting the fav icon and static folder app.use(express.favicon()); app.use(express.static(config.root + '/public')); diff --git a/config/routes.js b/config/routes.js index 2f26dc5..fc27b78 100644 --- a/config/routes.js +++ b/config/routes.js @@ -1,6 +1,6 @@ 'use strict'; -module.exports = function(app) { +module.exports = function(app, historicSync) { //Home route var index = require('../app/controllers/index'); @@ -29,4 +29,6 @@ module.exports = function(app) { var st = require('../app/controllers/status'); app.get('/api/status', st.show); + app.get('/api/sync', st.sync); + }; diff --git a/lib/PeerSync.js b/lib/PeerSync.js index 49526cf..3d7e89c 100644 --- a/lib/PeerSync.js +++ b/lib/PeerSync.js @@ -15,8 +15,6 @@ function spec() { PeerSync.prototype.init = function(config, cb) { if (!config) config = {}; - var that = this; - var network = config && (config.network || 'testnet'); this.verbose = config.verbose; @@ -71,6 +69,8 @@ function spec() { } this.sync.storeTxs([tx.hash], null, function(err) { if (err) { +console.log('[PeerSync.js.71:err:]',err); //TODO + console.log('[p2p_sync] Error in handle TX: ' + JSON.stringify(err)); } }); diff --git a/lib/Sync.js b/lib/Sync.js index 8561d4a..4a4ecdc 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -23,7 +23,7 @@ function spec() { if (!(opts && opts.skip_db_connection)) { - if (!mongoose.connection.readyState == 1) { + if (mongoose.connection.readyState !== 1) { mongoose.connect(config.db, function(err) { if (err) { console.log('CRITICAL ERROR: connecting to mongoDB:',err); diff --git a/server.js b/server.js index 38f607f..59303fd 100644 --- a/server.js +++ b/server.js @@ -24,7 +24,7 @@ var express = require('express'), var config = require('./config/config'); //Bootstrap db connection -var db = mongoose.connect(config.db); +mongoose.connect(config.db); //Bootstrap models var models_path = __dirname + '/app/models'; @@ -44,17 +44,18 @@ var walk = function(path) { walk(models_path); // historic_sync process +var historicSync = {}; if (!config.disableHistoricSync) { - var hs = new HistoricSync(); - hs.init({ + historicSync = new HistoricSync(); + historicSync.init({ skip_db_connection: true, networkName: config.network }, function() { - hs.smart_import(function(err){ + historicSync.smart_import(function(err){ var txt= 'ended.'; if (err) txt = 'ABORTED with error: ' + err.message; - console.log('[historic_sync] ' + txt, hs.syncInfo); + console.log('[historic_sync] ' + txt, historicSync.syncInfo); }); }); } @@ -77,7 +78,7 @@ if (!config.disableP2pSync) { var app = express(); //express settings -require('./config/express')(app, db); +require('./config/express')(app, historicSync); //Bootstrap routes require('./config/routes')(app);