diff --git a/src/Controllers/Controller.php b/src/Controllers/Controller.php new file mode 100644 index 0000000..fc22251 --- /dev/null +++ b/src/Controllers/Controller.php @@ -0,0 +1,19 @@ +container = $container; + } + + public function get(string $name) { + return $this->container->get($name); + } + +} diff --git a/src/Controllers/HomeController.php b/src/Controllers/HomeController.php new file mode 100644 index 0000000..67b22c5 --- /dev/null +++ b/src/Controllers/HomeController.php @@ -0,0 +1,30 @@ +get('config'); + + // 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)); + } + + $view = Twig::fromRequest($request); + return $view->render($response, 'index.twig', [ + 'servers' => $minecraftServers, + ]); + } + +} diff --git a/src/Controllers/ServerController.php b/src/Controllers/ServerController.php new file mode 100644 index 0000000..c3d4dd8 --- /dev/null +++ b/src/Controllers/ServerController.php @@ -0,0 +1,113 @@ +render($response, 'create.twig'); + } + + // POST create server route + public function postCreate(Request $request, Response $response, $args): Response { + // 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); + + // 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 + return $response + ->withHeader('Location', '/') + ->withStatus(302); + } + + // GET server status route + public function getStatus(Request $request, Response $response, $args): Response { + $config = $this->get('config'); + $serverDir = join('/', array($config->get('server_directory'), $args['serverName'])); + + // check if the server exists - if not, return a false result + if (!is_dir($serverDir)) { + $response->getBody()->write(json_encode(array('exists' => false))); + return $response + ->withHeader('Content-Type', 'application/json'); + } + + // create server object and pass info back to client as JSON data + $server = new Server($serverDir); + $serverData = [ + 'exists' => true, + 'name' => $server->getName(), + 'version' => $server->getVersion(), + 'state' => $server->getState() ? 'Running' : 'Stopped', + ]; + + $response->getBody()->write(json_encode($serverData)); + return $response + ->withHeader('Content-Type', 'application/json'); + } + + // GET server start route + public function getStart(Request $request, Response $response, $args): Response { + $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); + } + + // GET server stop route + public function getStop(Request $request, Response $response, $args): Response { + $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); + } + +} diff --git a/src/routes.php b/src/routes.php index f7c2332..45f4b97 100644 --- a/src/routes.php +++ b/src/routes.php @@ -7,119 +7,17 @@ use Slim\Views\Twig; use BitGoblin\MCST\Minecraft\Server; // 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'); - - // 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)); - } - - $view = Twig::fromRequest($request); - return $view->render($response, 'index.twig', [ - 'servers' => $minecraftServers, - ]); -})->setName('index'); +$app->get('/', '\\BitGoblin\\MCST\\Controllers\\HomeController:getIndex')->setName('index'); // 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'); - +$app->get('/create', '\\BitGoblin\\MCST\\Controllers\\ServerController:getCreate')->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); - - // 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 - return $response - ->withHeader('Location', '/') - ->withStatus(302); -}); +$app->post('/create', '\\BitGoblin\\MCST\\Controllers\\ServerController:postCreate'); // 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'])); - - // check if the server exists - if not, return a false result - if (!is_dir($serverDir)) { - $response->getBody()->write(json_encode(array('exists' => false))); - return $response - ->withHeader('Content-Type', 'application/json'); - } - - // create server object and pass info back to client as JSON data - $server = new Server($serverDir); - $serverData = [ - 'exists' => true, - 'name' => $server->getName(), - 'version' => $server->getVersion(), - 'state' => $server->getState() ? 'Running' : 'Stopped', - ]; - - $response->getBody()->write(json_encode($serverData)); - return $response - ->withHeader('Content-Type', 'application/json'); -})->setName('server.status'); +$app->get('/server/{serverName}/status', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStatus')->setName('server.status'); // 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); -})->setName('server.start'); - +$app->get('/server/{serverName}/start', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStart')->setName('server.start'); // 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); -})->setName('server.stop'); +$app->get('/server/{serverName}/stop', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStop')->setName('server.stop');