From cf96473432d657723758b6096a3f22efaaedf793 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Tue, 21 Feb 2017 10:24:08 +0300 Subject: [PATCH] added shepherd refresh call --- routes/shepherd.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/routes/shepherd.js b/routes/shepherd.js index 2ddd493..d714528 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -89,6 +89,9 @@ shepherd.get('/appconf', function(req, res, next) { var allcoinsInProgress = false; +/* + * params: userpass + */ shepherd.get('/allcoins', function(req, res, next) { if (!allcoinsInProgress) { allcoinsInProgress = true; @@ -222,6 +225,79 @@ shepherd.get('/allcoins', function(req, res, next) { } }); +/* + * params: userpass, coin, address + */ +shepherd.get('/refresh', function(req, res, next) { + var sessionKey = req.query.userpass, + coin = req.query.coin, + address = req.query.address, + _obj = { + 'msg': 'error', + 'result': 'error' + }, + outObj, + pubkey, + writeCache = function() { + fs.writeFile(iguanaDir + '/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { + if (err) { + return console.log(err); + } + + console.log('file ' + iguanaDir + '/cache-' + pubkey + '.json is updated'); + }); + }; + + request({ + url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/SuperNET/activehandle?userpass=' + sessionKey, + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + pubkey = JSON.parse(body).pubkey; + + if (fs.existsSync(iguanaDir + '/cache-' + pubkey + '.json')) { + outObj = JSON.parse(fs.readFileSync(iguanaDir + '/cache-' + pubkey + '.json', 'utf8')); + + if (outObj && !outObj.basilisk) { + outObj['basilisk'] = {}; + outObj['basilisk'][coin] = {}; + } else { + if (!outObj[coin]) { + outObj['basilisk'][coin][address] = {}; + } + } + } else { + outObj = { + basilisk: {} + }; + } + + var refreshUrl = 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address + + request({ + url: refreshUrl, + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + outObj.basilisk[coin][address].refresh = JSON.parse(body); + console.log(refreshUrl); + console.log(body); + + writeCache(); + res.end(JSON.stringify({ + 'msg': 'success', + 'result': iguanaDir + '/cache-' + pubkey + '.json updated' + })); + } + }); + + + } else { + res.end(JSON.stringify(_obj)); + } + }); +}); + shepherd.post('/debuglog', function(req, res) { var _herd = req.body.herdname, _lastNLines = req.body.lastLines,