40 lines
1.8 KiB
JavaScript
40 lines
1.8 KiB
JavaScript
const http = require('http');
|
|
const settings = require('./settings');
|
|
const expected = settings.block_page.genesis_block;
|
|
let state = {status:'offline',chain:'NRB',height:null};
|
|
function rpc(method, params) {
|
|
return new Promise((resolve,reject)=>{
|
|
const data=JSON.stringify({id:'nerbus-health',method,params});
|
|
const r=http.request({host:settings.wallet.host,port:settings.wallet.port,method:'POST',
|
|
auth:settings.wallet.username+':'+settings.wallet.password,
|
|
headers:{'Content-Type':'application/json','Content-Length':Buffer.byteLength(data)}}, res=>{
|
|
let raw='';res.on('data',d=>raw+=d);res.on('end',()=>{
|
|
try { const v=JSON.parse(raw);if(v.error) reject(Error('RPC error'));else resolve(v.result); }
|
|
catch(e){reject(e);}
|
|
});
|
|
});
|
|
r.setTimeout(3000,()=>r.destroy(Error('RPC timeout')));
|
|
r.on('error',reject);r.end(data);
|
|
});
|
|
}
|
|
async function probe() {
|
|
try {
|
|
if(await rpc('getblockhash',[0])!==expected) {
|
|
state={status:'wrong-network',chain:'NRB',height:null};return;
|
|
}
|
|
state={status:'online',chain:'NRB',height:await rpc('getblockcount',[])};
|
|
} catch (_) { state={status:'offline',chain:'NRB',height:null}; }
|
|
}
|
|
module.exports=function(app) {
|
|
probe();setInterval(probe,10000).unref();
|
|
app.get('/healthz',(req,res)=>res.json({explorer:'Nerbus',...state}));
|
|
app.use((req,res,next)=>{res.locals.nerbusState=state;
|
|
if(state.status!=='online' && req.path==='/ext/getsummary')
|
|
return res.json({status:state.status,difficulty:'-',difficultyHybrid:'',supply:'-',hashrate:'-',
|
|
connections:'-',blockcount:'-',lastPrice:'0',lastUSDPrice:'0',marketcap:'0.00',usdMarketcap:'0.00'});
|
|
if(state.status!=='online' && req.path.startsWith('/api/'))
|
|
return res.status(503).json({error:'Nerbus node is offline; network launch pending'});
|
|
next();
|
|
});
|
|
};
|