ethstats-server/app.js

135 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-02-17 06:12:44 +01:00
var express = require('express');
var app = express();
2014-12-03 04:08:49 +01:00
var path = require('path');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
2015-02-05 00:55:48 +01:00
2015-02-17 03:04:15 +01:00
var Primus = require('primus'),
api,
client;
2015-02-17 03:04:15 +01:00
var Collection = require('./models/collection');
var Nodes = new Collection();
2015-02-17 06:12:44 +01:00
var server = require('http').createServer(app);
2015-02-17 03:04:15 +01:00
api = new Primus(server, {
transformer: 'websockets',
pathname: '/api',
parser: 'JSON'
});
api.use('emit', require('primus-emit'));
2015-02-23 14:57:41 +01:00
api.use('spark-latency', require('primus-spark-latency'));
2015-02-17 03:04:15 +01:00
2015-02-17 06:12:44 +01:00
var client = new Primus(server, {
transformer: 'websockets',
pathname: '/primus',
parser: 'JSON'
});
client.use('emit', require('primus-emit'));
2015-02-17 03:04:15 +01:00
api.on('connection', function(spark) {
2015-02-23 14:57:41 +01:00
console.log('Latency: ', spark.latency);
2015-02-17 03:04:15 +01:00
console.log(spark.id);
console.log(spark.address);
console.log(spark.query);
2015-02-18 07:06:41 +01:00
spark.on('hello', function(data)
{
2015-02-23 14:57:41 +01:00
console.log('Latency: ', spark.latency);
2015-02-17 11:14:48 +01:00
console.log('got hello data from ', spark.id);
2015-02-17 03:04:15 +01:00
console.log(data);
2015-02-17 06:12:44 +01:00
if(typeof data.id !== 'undefined' && typeof data.info !== 'undefined')
{
data.ip = spark.address.ip;
2015-02-18 07:06:41 +01:00
data.spark = spark.id;
2015-02-17 06:12:44 +01:00
var info = Nodes.add(data);
spark.emit('ready');
2015-02-17 10:30:41 +01:00
client.write({action: 'add', data: info});
2015-02-17 06:12:44 +01:00
}
2015-02-17 03:04:15 +01:00
});
2015-02-18 07:06:41 +01:00
spark.on('update', function(data)
{
2015-02-23 14:57:41 +01:00
console.log('Latency: ', spark.latency);
2015-02-17 03:04:15 +01:00
console.log('got update from ' + spark.id);
console.log(data);
2015-02-17 06:12:44 +01:00
if(typeof data.id !== 'undefined' && typeof data.stats !== 'undefined')
{
2015-02-23 14:57:41 +01:00
data.stats.latency = spark.latency;
2015-02-17 06:12:44 +01:00
var stats = Nodes.update(data.id, data.stats);
2015-02-17 03:04:15 +01:00
2015-02-17 10:30:41 +01:00
client.write({action: 'update', data: stats});
2015-02-17 06:12:44 +01:00
}
});
2015-02-17 11:14:48 +01:00
2015-02-18 07:06:41 +01:00
spark.on('end', function(data)
{
var stats = Nodes.inactive(spark.id);
client.write({action: 'inactive', data: stats});
2015-02-17 11:14:48 +01:00
});
2015-02-17 06:12:44 +01:00
});
2015-02-17 03:04:15 +01:00
client.on('connection', function(spark) {
console.log(spark.id);
console.log(spark.address);
console.log(spark.query);
2015-02-17 06:12:44 +01:00
spark.on('ready', function(data){
console.log('got hello data from ' + spark.id);
console.log(data);
2014-12-03 04:08:49 +01:00
2015-02-17 06:12:44 +01:00
spark.emit('init', {nodes: Nodes.all()});
});
});
2014-12-03 04:08:49 +01:00
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
2015-02-17 06:12:44 +01:00
app.get('/', function(req, res) {
res.render('index', { title: 'Ethereum Network Status' });
});
2014-12-03 04:08:49 +01:00
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
2015-02-17 06:12:44 +01:00
server.listen(process.env.PORT || 3000);
2014-12-03 04:08:49 +01:00
module.exports = app;