Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receive enodes over websockets and serve them at /static-nodes.json #361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -46,7 +50,6 @@ var api;
var client;
var server;


// Init API Socket connection
api = new Primus(server, {
transformer: 'websockets',
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
});
Expand Down
12 changes: 11 additions & 1 deletion lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
Expand All @@ -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');
Expand All @@ -39,4 +49,4 @@ app.use(function(err, req, res, next) {
});
});

module.exports = app;
module.exports = app;