sync API
This commit is contained in:
parent
4d23191116
commit
08a54a40e2
|
@ -18,6 +18,7 @@ node_modules
|
||||||
|
|
||||||
# extras
|
# extras
|
||||||
*.swp
|
*.swp
|
||||||
|
*.swo
|
||||||
*~
|
*~
|
||||||
.project
|
.project
|
||||||
peerdb.json
|
peerdb.json
|
||||||
|
|
|
@ -48,10 +48,14 @@ exports.show = function(req, res, next) {
|
||||||
res.jsonp(d);
|
res.jsonp(d);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
res.status(400).send('Bad Request');
|
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'),
|
helpers = require('view-helpers'),
|
||||||
config = require('./config');
|
config = require('./config');
|
||||||
|
|
||||||
module.exports = function(app, passport, db) {
|
module.exports = function(app, historicSync) {
|
||||||
|
|
||||||
app.set('showStackError', true);
|
app.set('showStackError', true);
|
||||||
|
|
||||||
//Prettify HTML
|
//Prettify HTML
|
||||||
|
@ -26,9 +27,17 @@ module.exports = function(app, passport, db) {
|
||||||
app.set('view engine', 'jade');
|
app.set('view engine', 'jade');
|
||||||
|
|
||||||
//Enable jsonp
|
//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() {
|
app.configure(function() {
|
||||||
|
|
||||||
//cookieParser should be above session
|
//cookieParser should be above session
|
||||||
app.use(express.cookieParser());
|
app.use(express.cookieParser());
|
||||||
|
|
||||||
|
@ -43,6 +52,7 @@ module.exports = function(app, passport, db) {
|
||||||
//routes should be at the last
|
//routes should be at the last
|
||||||
app.use(app.router);
|
app.use(app.router);
|
||||||
|
|
||||||
|
|
||||||
//Setting the fav icon and static folder
|
//Setting the fav icon and static folder
|
||||||
app.use(express.favicon());
|
app.use(express.favicon());
|
||||||
app.use(express.static(config.root + '/public'));
|
app.use(express.static(config.root + '/public'));
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports = function(app) {
|
module.exports = function(app, historicSync) {
|
||||||
|
|
||||||
//Home route
|
//Home route
|
||||||
var index = require('../app/controllers/index');
|
var index = require('../app/controllers/index');
|
||||||
|
@ -29,4 +29,6 @@ module.exports = function(app) {
|
||||||
var st = require('../app/controllers/status');
|
var st = require('../app/controllers/status');
|
||||||
app.get('/api/status', st.show);
|
app.get('/api/status', st.show);
|
||||||
|
|
||||||
|
app.get('/api/sync', st.sync);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,8 +15,6 @@ function spec() {
|
||||||
|
|
||||||
PeerSync.prototype.init = function(config, cb) {
|
PeerSync.prototype.init = function(config, cb) {
|
||||||
if (!config) config = {};
|
if (!config) config = {};
|
||||||
var that = this;
|
|
||||||
|
|
||||||
var network = config && (config.network || 'testnet');
|
var network = config && (config.network || 'testnet');
|
||||||
|
|
||||||
this.verbose = config.verbose;
|
this.verbose = config.verbose;
|
||||||
|
@ -71,6 +69,8 @@ function spec() {
|
||||||
}
|
}
|
||||||
this.sync.storeTxs([tx.hash], null, function(err) {
|
this.sync.storeTxs([tx.hash], null, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
console.log('[PeerSync.js.71:err:]',err); //TODO
|
||||||
|
|
||||||
console.log('[p2p_sync] Error in handle TX: ' + JSON.stringify(err));
|
console.log('[p2p_sync] Error in handle TX: ' + JSON.stringify(err));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -23,7 +23,7 @@ function spec() {
|
||||||
if (!(opts && opts.skip_db_connection)) {
|
if (!(opts && opts.skip_db_connection)) {
|
||||||
|
|
||||||
|
|
||||||
if (!mongoose.connection.readyState == 1) {
|
if (mongoose.connection.readyState !== 1) {
|
||||||
mongoose.connect(config.db, function(err) {
|
mongoose.connect(config.db, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log('CRITICAL ERROR: connecting to mongoDB:',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');
|
var config = require('./config/config');
|
||||||
|
|
||||||
//Bootstrap db connection
|
//Bootstrap db connection
|
||||||
var db = mongoose.connect(config.db);
|
mongoose.connect(config.db);
|
||||||
|
|
||||||
//Bootstrap models
|
//Bootstrap models
|
||||||
var models_path = __dirname + '/app/models';
|
var models_path = __dirname + '/app/models';
|
||||||
|
@ -44,17 +44,18 @@ var walk = function(path) {
|
||||||
walk(models_path);
|
walk(models_path);
|
||||||
|
|
||||||
// historic_sync process
|
// historic_sync process
|
||||||
|
var historicSync = {};
|
||||||
if (!config.disableHistoricSync) {
|
if (!config.disableHistoricSync) {
|
||||||
var hs = new HistoricSync();
|
historicSync = new HistoricSync();
|
||||||
hs.init({
|
historicSync.init({
|
||||||
skip_db_connection: true,
|
skip_db_connection: true,
|
||||||
networkName: config.network
|
networkName: config.network
|
||||||
}, function() {
|
}, function() {
|
||||||
hs.smart_import(function(err){
|
historicSync.smart_import(function(err){
|
||||||
var txt= 'ended.';
|
var txt= 'ended.';
|
||||||
if (err) txt = 'ABORTED with error: ' + err.message;
|
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();
|
var app = express();
|
||||||
|
|
||||||
//express settings
|
//express settings
|
||||||
require('./config/express')(app, db);
|
require('./config/express')(app, historicSync);
|
||||||
|
|
||||||
//Bootstrap routes
|
//Bootstrap routes
|
||||||
require('./config/routes')(app);
|
require('./config/routes')(app);
|
||||||
|
|
Loading…
Reference in New Issue