z-nomp/libs/website.js

291 lines
9.3 KiB
JavaScript
Raw Normal View History

2014-03-12 23:37:27 -07:00
2014-03-14 00:18:51 -07:00
var fs = require('fs');
var path = require('path');
2014-03-12 23:37:27 -07:00
2014-03-14 00:18:51 -07:00
var async = require('async');
var watch = require('node-watch');
var redis = require('redis');
2014-03-12 23:37:27 -07:00
var dot = require('dot');
var express = require('express');
var bodyParser = require('body-parser');
var compress = require('compression');
2014-03-12 23:37:27 -07:00
var Stratum = require('stratum-pool');
var util = require('stratum-pool/lib/util.js');
var api = require('./api.js');
2014-03-12 23:37:27 -07:00
2014-03-13 14:03:28 -07:00
2014-03-12 23:37:27 -07:00
module.exports = function(logger){
dot.templateSettings.strip = false;
2014-03-12 23:37:27 -07:00
var portalConfig = JSON.parse(process.env.portalConfig);
var poolConfigs = JSON.parse(process.env.pools);
2014-03-18 23:54:18 -07:00
var websiteConfig = portalConfig.website;
2014-03-14 00:18:51 -07:00
var portalApi = new api(logger, portalConfig, poolConfigs);
var portalStats = portalApi.stats;
2014-03-14 00:18:51 -07:00
var logSystem = 'Website';
2014-03-12 23:37:27 -07:00
2014-03-18 23:54:18 -07:00
var pageFiles = {
'index.html': 'index',
'home.html': '',
'getting_started.html': 'getting_started',
'stats.html': 'stats',
'tbs.html': 'tbs',
2014-05-13 09:59:13 -07:00
'workers.html': 'workers',
'api.html': 'api',
'admin.html': 'admin',
'mining_key.html': 'mining_key'
2014-03-14 00:18:51 -07:00
};
2014-03-18 23:54:18 -07:00
var pageTemplates = {};
var pageProcessed = {};
2014-03-20 15:25:59 -07:00
var indexesProcessed = {};
2014-03-14 00:18:51 -07:00
var keyScriptTemplate = '';
var keyScriptProcessed = '';
2014-03-14 00:18:51 -07:00
2014-03-18 23:54:18 -07:00
var processTemplates = function(){
2014-03-20 15:25:59 -07:00
2014-03-18 23:54:18 -07:00
for (var pageName in pageTemplates){
2014-03-20 15:25:59 -07:00
if (pageName === 'index') continue;
2014-03-18 23:54:18 -07:00
pageProcessed[pageName] = pageTemplates[pageName]({
poolsConfigs: poolConfigs,
2014-03-19 13:24:29 -07:00
stats: portalStats.stats,
2014-03-19 00:26:20 -07:00
portalConfig: portalConfig
2014-03-18 23:54:18 -07:00
});
2014-03-20 15:25:59 -07:00
indexesProcessed[pageName] = pageTemplates.index({
page: pageProcessed[pageName],
selected: pageName,
stats: portalStats.stats,
poolConfigs: poolConfigs,
portalConfig: portalConfig
});
2014-03-18 23:54:18 -07:00
}
2014-03-26 19:53:30 -07:00
//logger.debug(logSystem, 'Stats', 'Website updated to latest stats');
2014-03-18 23:54:18 -07:00
};
2014-03-14 00:18:51 -07:00
var readPageFiles = function(files){
async.each(files, function(fileName, callback){
2014-03-19 13:36:44 -07:00
var filePath = 'website/' + (fileName === 'index.html' ? '' : 'pages/') + fileName;
fs.readFile(filePath, 'utf8', function(err, data){
2014-03-18 23:54:18 -07:00
var pTemp = dot.template(data);
pageTemplates[pageFiles[fileName]] = pTemp
callback();
});
}, function(err){
if (err){
console.log('error reading files for creating dot templates: '+ JSON.stringify(err));
return;
}
processTemplates();
2014-03-14 00:18:51 -07:00
});
};
//If an html file was changed reload it
watch('website', function(filename){
var basename = path.basename(filename);
if (basename in pageFiles){
console.log(filename);
readPageFiles([basename]);
logger.debug(logSystem, 'Server', 'Reloaded file ' + basename);
}
2014-03-18 23:54:18 -07:00
});
portalStats.getGlobalStats(function(){
2014-03-18 23:54:18 -07:00
readPageFiles(Object.keys(pageFiles));
2014-03-14 00:18:51 -07:00
});
2014-03-18 23:54:18 -07:00
var buildUpdatedWebsite = function(){
portalStats.getGlobalStats(function(){
2014-03-18 23:54:18 -07:00
processTemplates();
2014-03-19 13:24:29 -07:00
var statData = 'data: ' + JSON.stringify(portalStats.stats) + '\n\n';
for (var uid in portalApi.liveStatConnections){
var res = portalApi.liveStatConnections[uid];
2014-03-19 13:24:29 -07:00
res.write(statData);
}
2014-03-18 23:54:18 -07:00
});
};
setInterval(buildUpdatedWebsite, websiteConfig.stats.updateInterval * 1000);
2014-03-18 23:54:18 -07:00
2014-03-14 00:18:51 -07:00
var buildKeyScriptPage = function(){
async.waterfall([
function(callback){
var client = redis.createClient(portalConfig.redis.port, portalConfig.redis.host);
client.hgetall('coinVersionBytes', function(err, coinBytes){
if (err){
client.quit();
return callback('Failed grabbing coin version bytes from redis ' + JSON.stringify(err));
}
callback(null, client, coinBytes || {});
});
},
function (client, coinBytes, callback){
var enabledCoins = Object.keys(poolConfigs).map(function(c){return c.toLowerCase()});
var missingCoins = [];
enabledCoins.forEach(function(c){
if (!(c in coinBytes))
missingCoins.push(c);
});
callback(null, client, coinBytes, missingCoins);
},
function(client, coinBytes, missingCoins, callback){
var coinsForRedis = {};
async.each(missingCoins, function(c, cback){
var coinInfo = (function(){
for (var pName in poolConfigs){
if (pName.toLowerCase() === c)
return {
daemon: poolConfigs[pName].paymentProcessing.daemon,
address: poolConfigs[pName].address
}
}
})();
var daemon = new Stratum.daemon.interface([coinInfo.daemon], function(severity, message){
logger[severity](logSystem, c, message);
});
daemon.cmd('dumpprivkey', [coinInfo.address], function(result){
if (result[0].error){
logger.error(logSystem, c, 'Could not dumpprivkey for ' + c + ' ' + JSON.stringify(result[0].error));
cback();
return;
}
var vBytePub = util.getVersionByte(coinInfo.address)[0];
var vBytePriv = util.getVersionByte(result[0].response)[0];
coinBytes[c] = vBytePub.toString() + ',' + vBytePriv.toString();
coinsForRedis[c] = coinBytes[c];
cback();
});
}, function(err){
callback(null, client, coinBytes, coinsForRedis);
});
},
function(client, coinBytes, coinsForRedis, callback){
if (Object.keys(coinsForRedis).length > 0){
client.hmset('coinVersionBytes', coinsForRedis, function(err){
if (err)
logger.error(logSystem, 'Init', 'Failed inserting coin byte version into redis ' + JSON.stringify(err));
client.quit();
});
}
else{
client.quit();
}
callback(null, coinBytes);
}
], function(err, coinBytes){
if (err){
logger.error(logSystem, 'Init', err);
return;
}
try{
keyScriptTemplate = dot.template(fs.readFileSync('website/key.html', {encoding: 'utf8'}));
keyScriptProcessed = keyScriptTemplate({coins: coinBytes});
}
catch(e){
logger.error(logSystem, 'Init', 'Failed to read key.html file');
}
});
};
buildKeyScriptPage();
2014-03-12 23:37:27 -07:00
2014-03-18 23:54:18 -07:00
var getPage = function(pageId){
if (pageId in pageProcessed){
var requestedPage = pageProcessed[pageId];
return requestedPage;
}
};
var route = function(req, res, next){
var pageId = req.params.page || '';
2014-03-20 15:25:59 -07:00
if (pageId in indexesProcessed){
res.header('Content-Type', 'text/html');
2014-03-20 15:25:59 -07:00
res.end(indexesProcessed[pageId]);
2014-03-18 23:54:18 -07:00
}
else
next();
2014-03-14 00:18:51 -07:00
2014-03-18 23:54:18 -07:00
};
2014-03-14 00:18:51 -07:00
2014-03-19 13:24:29 -07:00
var app = express();
app.use(bodyParser.json());
app.get('/get_page', function(req, res, next){
var requestedPage = getPage(req.query.id);
if (requestedPage){
res.end(requestedPage);
return;
}
next();
});
2014-03-19 13:24:29 -07:00
2014-05-12 14:17:00 -07:00
app.get('/key.html', function(req, res, next){
res.end(keyScriptProcessed);
});
2014-03-18 23:54:18 -07:00
app.get('/:page', route);
app.get('/', route);
2014-03-14 00:18:51 -07:00
2014-03-18 23:54:18 -07:00
app.get('/api/:method', function(req, res, next){
portalApi.handleApiRequest(req, res, next);
2014-03-12 23:37:27 -07:00
});
app.post('/api/admin/:method', function(req, res, next){
if (portalConfig.website
&& portalConfig.website.adminCenter
&& portalConfig.website.adminCenter.enabled){
if (portalConfig.website.adminCenter.password === req.body.password)
portalApi.handleAdminApiRequest(req, res, next);
else
res.send(401, JSON.stringify({error: 'Incorrect Password'}));
}
else
next();
});
app.use(compress());
2014-03-19 13:36:44 -07:00
app.use('/static', express.static('website/static'));
2014-03-18 23:54:18 -07:00
2014-03-12 23:37:27 -07:00
app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
2014-03-12 23:37:27 -07:00
});
try {
2014-05-09 04:41:16 -07:00
app.listen(portalConfig.website.port, portalConfig.website.host, function () {
logger.debug(logSystem, 'Server', 'Website started on ' + portalConfig.website.host + ':' + portalConfig.website.port);
});
}
catch(e){
2014-05-09 04:41:16 -07:00
logger.error(logSystem, 'Server', 'Could not start website on ' + portalConfig.website.host + ':' + portalConfig.website.port
+ ' - its either in use or you do not have permission');
}
2014-03-12 23:37:27 -07:00
};