Refactored routes to be under MVC controllers

This commit is contained in:
Gregory Ballantine 2022-10-07 17:47:10 -04:00
parent 54199bf7c8
commit bb584d5ea0
4 changed files with 168 additions and 108 deletions

View File

@ -0,0 +1,19 @@
<?php
namespace BitGoblin\MCST\Controllers;
use Psr\Container\ContainerInterface;
class Controller {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function get(string $name) {
return $this->container->get($name);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace BitGoblin\MCST\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
use BitGoblin\MCST\Minecraft\Server;
class HomeController extends Controller {
public function getIndex(Request $request, Response $response, $args): Response {
$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,
]);
}
}

View File

@ -0,0 +1,113 @@
<?php
namespace BitGoblin\MCST\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
use BitGoblin\MCST\Minecraft\Server;
class ServerController extends Controller {
// GET create server route
public function getCreate(Request $request, Response $response, $args): Response {
$view = Twig::fromRequest($request);
return $view->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);
}
}

View File

@ -7,119 +7,17 @@ use Slim\Views\Twig;
use BitGoblin\MCST\Minecraft\Server; use BitGoblin\MCST\Minecraft\Server;
// index GET route - this page should welcome the user and direct them to the available actions // index GET route - this page should welcome the user and direct them to the available actions
$app->get('/', function (Request $request, Response $response, $args) { $app->get('/', '\\BitGoblin\\MCST\\Controllers\\HomeController:getIndex')->setName('index');
$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');
// create GET route - this page allows a user to create a new server instance // create GET route - this page allows a user to create a new server instance
$app->get('/create', function (Request $request, Response $response, $args) { $app->get('/create', '\\BitGoblin\\MCST\\Controllers\\ServerController:getCreate')->setName('create');
$view = Twig::fromRequest($request);
return $view->render($response, 'create.twig');
})->setName('create');
// create POST route - processes the new server creation // create POST route - processes the new server creation
$app->post('/create', function (Request $request, Response $response, $args) { $app->post('/create', '\\BitGoblin\\MCST\\Controllers\\ServerController:postCreate');
// 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);
});
// server status route // server status route
$app->get('/server/{serverName}/status', function (Request $request, Response $response, $args) { $app->get('/server/{serverName}/status', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStatus')->setName('server.status');
$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');
// server start route // server start route
$app->get('/server/{serverName}/start', function (Request $request, Response $response, $args) { $app->get('/server/{serverName}/start', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStart')->setName('server.start');
$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');
// server stop route // server stop route
$app->get('/server/{serverName}/stop', function (Request $request, Response $response, $args) { $app->get('/server/{serverName}/stop', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStop')->setName('server.stop');
$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');