diff --git a/app.js b/app.js index 676cfdb8..c890c529 100644 --- a/app.js +++ b/app.js @@ -2,10 +2,14 @@ var _ = require('lodash'); var logger = require('./lib/utils/logger'); var chalk = require('chalk'); var http = require('http'); +var fs = require('fs'); // Init WS SECRET var WS_SECRET; +STATIC_NODES_JSON = 'static-nodes.json'; +exports.STATIC_NODES_JSON = STATIC_NODES_JSON; + if( !_.isUndefined(process.env.WS_SECRET) && !_.isNull(process.env.WS_SECRET) ) { if( process.env.WS_SECRET.indexOf('|') > 0 ) @@ -46,7 +50,6 @@ var api; var client; var server; - // Init API Socket connection api = new Primus(server, { transformer: 'websockets', @@ -81,6 +84,9 @@ external.plugin('emit', require('primus-emit')); var Collection = require('./lib/collection'); var Nodes = new Collection(external); +// Node Id to Enode mapping +var enodes = {}; + Nodes.setChartsCallback(function (err, charts) { if(err !== null) @@ -268,7 +274,12 @@ api.on('connection', function (spark) console.error('API', 'STA', 'Stats error:', data); } }); - + spark.on('enode', function (data) + { + console.success('API', 'EN', 'Enode from:', data.id); + enodes[data.id] = data.enode + fs.writeFileSync(STATIC_NODES_JSON, JSON.stringify(Object.values(enodes))); + }); spark.on('history', function (data) { @@ -371,7 +382,7 @@ client.on('connection', function (clientSpark) { clientSpark.on('ready', function (data) { - clientSpark.emit('init', { nodes: Nodes.all() }); + clientSpark.emit('init', { nodes: Nodes.all()}); Nodes.getCharts(); }); diff --git a/lib/express.js b/lib/express.js index bd37f5fd..5b1b83e7 100644 --- a/lib/express.js +++ b/lib/express.js @@ -2,6 +2,8 @@ var express = require('express'); var app = express(); var path = require('path'); var bodyParser = require('body-parser'); +var fs = require('fs'); +const { STATIC_NODES_JSON } = require('../app.js'); // view engine setup app.set('views', path.join(__dirname, (process.env.LITE === 'true' ? '../src-lite/views' : '../src/views'))); @@ -14,6 +16,14 @@ app.get('/', function(req, res) { res.render('index'); }); +app.get('/static-nodes.json', function(req, res) { + var stream = fs.createReadStream(STATIC_NODES_JSON, {bufferSize: 64 * 1024}) + stream.on('error', () => { + res.send('[]'); + }); + stream.pipe(res); +}); + // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); @@ -39,4 +49,4 @@ app.use(function(err, req, res, next) { }); }); -module.exports = app; \ No newline at end of file +module.exports = app;