2022-09-22 22:17:41 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
use Slim\Views\Twig;
|
|
|
|
|
2022-09-24 16:26:57 -04:00
|
|
|
use BitGoblin\MCST\Minecraft\Server;
|
|
|
|
|
2022-09-22 22:17:41 -04:00
|
|
|
// index GET route - this page should welcome the user and direct them to the available actions
|
|
|
|
$app->get('/', function (Request $request, Response $response, $args) {
|
|
|
|
$config = $this->get('config');
|
|
|
|
|
2022-09-24 16:26:57 -04:00
|
|
|
// find servers
|
|
|
|
$minecraftDir = $config->get('server_directory');
|
|
|
|
$serverDirs = glob($minecraftDir . "/*", GLOB_ONLYDIR);
|
|
|
|
$minecraftServers = array();
|
|
|
|
foreach ($serverDirs as $i => $m) {
|
|
|
|
array_push($minecraftServers, new Server($m));
|
|
|
|
}
|
|
|
|
|
2022-09-22 22:17:41 -04:00
|
|
|
$view = Twig::fromRequest($request);
|
|
|
|
return $view->render($response, 'index.twig', [
|
2022-09-24 16:26:57 -04:00
|
|
|
'servers' => $minecraftServers,
|
2022-09-22 22:17:41 -04:00
|
|
|
]);
|
|
|
|
})->setName('index');
|
2022-09-22 23:28:06 -04:00
|
|
|
|
|
|
|
// create GET route - this page allows a user to create a new server instance
|
|
|
|
$app->get('/create', function (Request $request, Response $response, $args) {
|
|
|
|
$view = Twig::fromRequest($request);
|
|
|
|
return $view->render($response, 'create.twig');
|
|
|
|
})->setName('create');
|
|
|
|
|
|
|
|
// create POST route - processes the new server creation
|
|
|
|
$app->post('/create', function (Request $request, Response $response, $args) {
|
|
|
|
// set up the POST parameters and grab our config
|
|
|
|
$params = (array)$request->getParsedBody();
|
|
|
|
$config = $this->get('config');
|
|
|
|
|
|
|
|
// create our server directory
|
|
|
|
$serverDir = join('/', array($config->get('server_directory'), $params['serverName']));
|
|
|
|
mkdir($serverDir);
|
|
|
|
|
|
|
|
// grab the server JAR file
|
|
|
|
$serverJarUrl = "https://piston-data.mojang.com/v1/objects/f69c284232d7c7580bd89a5a4931c3581eae1378/server.jar";
|
|
|
|
$serverJarName = "server_" . $params['serverVersion'] . ".jar";
|
|
|
|
$serverJarPath = join('/', array($serverDir, $serverJarName));
|
|
|
|
file_put_contents($serverJarPath, file_get_contents($serverJarUrl));
|
|
|
|
|
|
|
|
// create the start.sh shell script
|
|
|
|
$scriptFilePath = join('/', array($serverDir, 'start.sh'));
|
|
|
|
$scriptFile = fopen($scriptFilePath, 'w');
|
|
|
|
$scriptContent = "#!/bin/sh\n\ncd " . $serverDir . "\njava -Xmx2048M -Xms2048M -jar server_1.19.2.jar nogui";
|
|
|
|
fwrite($scriptFile, $scriptContent);
|
|
|
|
fclose($scriptFile);
|
|
|
|
chmod($scriptFilePath, 0755);
|
|
|
|
|
2022-09-24 17:33:59 -04:00
|
|
|
// save the current version to a text file - this will hopefully be temporary solution
|
|
|
|
$versionFilePath = join('/', array($serverDir, 'current_version.txt'));
|
|
|
|
$versionFile = fopen($versionFilePath, 'w');
|
|
|
|
$versionContent = $params['serverVersion'];
|
|
|
|
fwrite($versionFile, $versionContent);
|
|
|
|
fclose($versionFile);
|
|
|
|
chmod($versionFilePath, 0644);
|
|
|
|
|
|
|
|
// redirect the user back to the home page
|
2022-09-22 23:28:06 -04:00
|
|
|
return $response
|
|
|
|
->withHeader('Location', '/')
|
|
|
|
->withStatus(302);
|
|
|
|
});
|
2022-09-24 20:45:37 -04:00
|
|
|
|
2022-09-24 21:29:12 -04:00
|
|
|
// 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 = [
|
2022-09-24 22:10:23 -04:00
|
|
|
'name' => $server->getName(),
|
|
|
|
'version' => $server->getVersion(),
|
|
|
|
'state' => $server->getState() ? 'Running' : 'Stopped',
|
2022-09-24 21:29:12 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
$response->getBody()->write(json_encode($serverData));
|
|
|
|
return $response
|
|
|
|
->withHeader('Content-Type', 'application/json');
|
|
|
|
})->setName('server.status');
|
|
|
|
|
2022-09-24 20:45:37 -04:00
|
|
|
// server start route
|
|
|
|
$app->get('/server/{serverName}/start', function (Request $request, Response $response, $args) {
|
|
|
|
$config = $this->get('config');
|
|
|
|
$serverDir = join('/', array($config->get('server_directory'), $args['serverName']));
|
|
|
|
|
|
|
|
// create server object and start it
|
|
|
|
$server = new Server($serverDir);
|
|
|
|
$server->start();
|
|
|
|
|
|
|
|
// redirect the user back to the home page
|
|
|
|
return $response
|
|
|
|
->withHeader('Location', '/')
|
|
|
|
->withStatus(302);
|
2022-09-24 21:29:12 -04:00
|
|
|
})->setName('server.start');
|
2022-09-24 20:45:37 -04:00
|
|
|
|
|
|
|
// server stop route
|
|
|
|
$app->get('/server/{serverName}/stop', function (Request $request, Response $response, $args) {
|
|
|
|
$config = $this->get('config');
|
|
|
|
$serverDir = join('/', array($config->get('server_directory'), $args['serverName']));
|
|
|
|
|
|
|
|
// create server object and start it
|
|
|
|
$server = new Server($serverDir);
|
|
|
|
$server->stop();
|
|
|
|
|
|
|
|
// redirect the user back to the home page
|
|
|
|
return $response
|
|
|
|
->withHeader('Location', '/')
|
|
|
|
->withStatus(302);
|
2022-09-24 21:29:12 -04:00
|
|
|
})->setName('server.stop');
|