sync API
This commit is contained in:
parent
4d23191116
commit
08a54a40e2
|
@ -18,6 +18,7 @@ node_modules
|
|||
|
||||
# extras
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
.project
|
||||
peerdb.json
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
|
|
@ -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'));
|
||||
|
|
|
@ -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);
|
||||
|
||||
};
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
|
|
13
server.js
13
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);
|
||||
|
|
Loading…
Reference in New Issue