From 2043e733c9d1a2ad5c9f4a91a422b13656e12f05 Mon Sep 17 00:00:00 2001 From: kaotisk Date: Mon, 17 Jun 2024 05:12:50 +0300 Subject: Refactoring --- api/routes/getZLatest/index.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'api/routes/getZLatest/index.js') diff --git a/api/routes/getZLatest/index.js b/api/routes/getZLatest/index.js index 4dc1899..9141a4e 100644 --- a/api/routes/getZLatest/index.js +++ b/api/routes/getZLatest/index.js @@ -1,28 +1,39 @@ const { spawn } = require('child_process'); /* - * Gets the local latest zblock + * Gets the local latest zblock AKA zlatest * * Returns: * - JSON object * { zlatest: "Qm..." } * */ -module.exports = (req, res) => { +function getZLatest(req, res) +{ const command = spawn("ak-get-zlatest"); + var buffer = ""; command.stdout.on("data", data => { - res.send({zlatest:`${data}`}); + buffer += data; }); command.stderr.on("data", data => { - console.log(`stderr: ${data}`); + console.log(`stderr: ${data}`); }); command.on('error', (error) => { - console.log(`error: ${error.message}`); + console.log(`error: ${error.message}`); }); command.on("close", code => { - console.log(`child process exited with code ${code}`); + console.log(`child process exited with code ${code}`); + if (code === 0){ + res.writeHead(200, {'Content-Type': 'application/json'}); + res.end(JSON.stringify({zlatest:`${buffer}`})); + } else { + res.writeHead(404, {'Content-Type': 'application/json'}); + res.end({"error":"unreachable"}); + } }); -}; \ No newline at end of file +} + +module.exports = getZLatest; -- cgit v1.2.3