Example #1
0
});
/**
 * Get-request to /api/traceroute/:id gets the traceroute-data for a given id
 * from the file or DB.
 *
 * @param  {int}  $id   The id to get the location for.
 * @return {json}       Returns the traceroute-data in a json-object.
 */
$app->get('/api/traceroute/:id', function ($id) {
    // Add id to the response
    $out = array();
    $out['id'] = $id;
    $db = new Database();
    // Check if traceroute is marked as finished in the DB. If that's the case,
    // simply return the data and set in progress to false
    if ($db->tracerouteFinished($id)) {
        $result = $db->getTraceroute($id);
        $out['data'] = $result;
        $out['inProgress'] = false;
        echo json_encode($out);
        exit;
    } else {
        // If the traceroute command is not marked as finished, read the PID from
        // the file and check if it is still running
        $file = fopen('traceroutes/' . $id . '.pid', 'r');
        $pid = fgets($file);
        fclose($file);
        $out['data'] = array();
        if (isRunning($pid)) {
            $out['inProgress'] = true;
        } else {