Added some AJAX to dynamically update the server's run status

This commit is contained in:
2022-09-24 21:29:12 -04:00
parent 5699c9cf6a
commit efde09025c
3 changed files with 35 additions and 10 deletions

View File

@ -68,6 +68,22 @@ $app->post('/create', function (Request $request, Response $response, $args) {
->withStatus(302);
});
// server status route
$app->get('/server/{serverName}/status', function (Request $request, Response $response, $args) {
$config = $this->get('config');
$serverDir = join('/', array($config->get('server_directory'), $args['serverName']));
// create server object and pass info back to client as JSON data
$server = new Server($serverDir);
$serverData = [
'state' => $server->getState() ? 'Running' : 'Stopped',
];
$response->getBody()->write(json_encode($serverData));
return $response
->withHeader('Content-Type', 'application/json');
})->setName('server.status');
// server start route
$app->get('/server/{serverName}/start', function (Request $request, Response $response, $args) {
$config = $this->get('config');
@ -81,7 +97,7 @@ $app->get('/server/{serverName}/start', function (Request $request, Response $re
return $response
->withHeader('Location', '/')
->withStatus(302);
})->setName('start');
})->setName('server.start');
// server stop route
$app->get('/server/{serverName}/stop', function (Request $request, Response $response, $args) {
@ -96,4 +112,4 @@ $app->get('/server/{serverName}/stop', function (Request $request, Response $res
return $response
->withHeader('Location', '/')
->withStatus(302);
})->setName('stop');
})->setName('server.stop');