diff --git a/api/public/index.php b/api/public/index.php index 7a653e7..416e4ee 100644 --- a/api/public/index.php +++ b/api/public/index.php @@ -16,7 +16,6 @@ require __DIR__ . '/../vendor/autoload.php'; $dotenv = Dotenv::createImmutable(__DIR__ . '/../'); $dotenv->load(); -$domain = $_ENV['MAIN_COOKIE_DOMAIN'] ?? '.lxdapp.local'; session_set_cookie_params([ 'lifetime' => 0, 'path' => '/', @@ -33,7 +32,6 @@ if (session_status() === PHP_SESSION_NONE) { $containerBuilder = new ContainerBuilder(); $containerBuilder->useAutowiring(true); // Enable autowiring globally - // Add settings $settings = require __DIR__ . '/../src/Settings/Settings.php'; $settings($containerBuilder); @@ -58,7 +56,6 @@ $app->add(CorsMiddleware::class); // Register middleware (require __DIR__ . '/../src/Bootstrap/Middleware.php')($app); - // Register routes // API contianer proxy route diff --git a/api/src/Bootstrap/Middleware.php b/api/src/Bootstrap/Middleware.php index 7602cac..64d457d 100644 --- a/api/src/Bootstrap/Middleware.php +++ b/api/src/Bootstrap/Middleware.php @@ -2,7 +2,6 @@ use Slim\App; use Slim\Middleware\ErrorMiddleware; -use Slim\Middleware\BodyParsingMiddleware; return function (App $app) { // Add body parsing middleware (for JSON, form, etc.) diff --git a/api/src/Controllers/LoginController.php b/api/src/Controllers/LoginController.php index f055c86..8622b18 100644 --- a/api/src/Controllers/LoginController.php +++ b/api/src/Controllers/LoginController.php @@ -82,11 +82,10 @@ class LoginController ]); } - $ip = $lxd->getContainerIP($name); $nginx = $lxd->getContainerServiceStatus($name, 'nginx'); $mysql = $lxd->getContainerServiceStatus($name, 'mariadb'); - if ($ip && $nginx === 'active' && $mysql === 'active') { + if ($nginx === 'active' && $mysql === 'active') { // ---- CHANGED: do NOT return fields/creds here ---- return $this->json($response, [ 'status' => 'ready', diff --git a/api/src/Scripts/auto-stop-containers.php b/api/src/Scripts/auto-stop-containers.php deleted file mode 100755 index 5d3d893..0000000 --- a/api/src/Scripts/auto-stop-containers.php +++ /dev/null @@ -1,47 +0,0 @@ - $thresholdMinutes * 60) { - echo "$containerName has been idle for over $thresholdMinutes minutes. Stopping...\n"; - - try { - // Check if the container exists and stop it if it does - if ($lxdService->containerExists($containerName)) { - $lxdService->stopContainer($containerName); - echo "Stopped container: $containerName\n"; - } else { - echo "Container $containerName does not exist.\n"; - } - } catch (Throwable $e) { - echo "Error stopping $containerName: " . $e->getMessage() . "\n"; - } - } -} diff --git a/api/src/Services/LxdService.php b/api/src/Services/LxdService.php index d6ae289..889254f 100644 --- a/api/src/Services/LxdService.php +++ b/api/src/Services/LxdService.php @@ -10,23 +10,8 @@ class LxdService $this->baseUrl = $_ENV['LXD_API_URL'] ?? 'https://localhost:8443'; } - - - /** - * Sends an HTTP request to the LXD API. - * - * @param string $method HTTP method (GET, POST, PUT, etc.) - * @param string $endpoint API endpoint - * @param array $body Request body (optional) - * @return array Response from the API - * @throws Exception - */ - private function request(string $method, string $endpoint, array $body = []): array { - // if (!isset($_ENV['LXD_CLIENT_CERT'], $_ENV['LXD_CLIENT_KEY'])) { - // throw new \Exception("LXD_CLIENT_CERT and LXD_CLIENT_KEY must be set in .env"); - // } - - $ch = curl_init("{$this->baseUrl}{$endpoint}"); + private function curlInit($url, $method = '') { + $ch = curl_init($url); // Paths to client certificate and key for TLS authentication $clientCert = $_ENV['LXD_CLIENT_CERT'] ?? '/etc/ssl/lxdapp/client.crt'; @@ -38,13 +23,25 @@ class LxdService curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); - + if($method) curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); + return $ch; + } + + /** + * Sends an HTTP request to the LXD API. + * + * @param string $method HTTP method (GET, POST, PUT, etc.) + * @param string $endpoint API endpoint + * @param array $body Request body (optional) + * @return array Response from the API + * @throws Exception + */ + private function request(string $method, string $endpoint, array $body = []): array { + $ch = $this->curlInit("{$this->baseUrl}{$endpoint}", $method); if (!empty($body)) { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); } - $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); @@ -77,17 +74,7 @@ class LxdService */ private function requestRaw(string $endpoint): string { $url = "{$this->baseUrl}{$endpoint}"; - $ch = curl_init($url); - - $clientCert = $_ENV['LXD_CLIENT_CERT'] ?? '/etc/ssl/lxdapp/client.crt'; - $clientKey = $_ENV['LXD_CLIENT_KEY'] ?? '/etc/ssl/lxdapp/client.key'; - - curl_setopt($ch, CURLOPT_SSLCERT, $clientCert); - curl_setopt($ch, CURLOPT_SSLKEY, $clientKey); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); + $ch = $this->curlInit($url); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); diff --git a/index.php b/index.php index ee7f6e5..7d7d3f0 100644 --- a/index.php +++ b/index.php @@ -41,8 +41,6 @@ if ($state !== 'Running') { } // === Get container IP === -$ip = $lxd->getContainerIP($container); - $nginx = $lxd->getContainerServiceStatus($container, 'nginx'); $mysql = $lxd->getContainerServiceStatus($container, 'mariadb');