aboutsummaryrefslogtreecommitdiff
path: root/api/routes/getZLatest/index.js
blob: 9141a4e63d11ea40330059f99ebe6fc1fda9d124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const { spawn } = require('child_process');

/*
 * Gets the local latest zblock AKA zlatest
 *
 * Returns:
 *     - JSON object
 *     { zlatest: "Qm..." }
 *
 */
function getZLatest(req, res)
{
    const command = spawn("ak-get-zlatest");
    var buffer = "";
    command.stdout.on("data", data => {
        buffer += data;
    });

    command.stderr.on("data", data => {
        console.log(`stderr: ${data}`);
    });

    command.on('error', (error) => {
        console.log(`error: ${error.message}`);
    });

    command.on("close", 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"});
        }
    });
}

module.exports = getZLatest;