Refactor code

This commit is contained in:
2025-07-09 08:30:46 +02:00
parent 311cb4e4fa
commit 4aa3a766c6
8 changed files with 193 additions and 104 deletions

View File

@ -12,12 +12,15 @@ use App\lib\PCaptcha;
class ProxyController
{
/**
* Handles forwarding requests to the appropriate container.
*/
public function forward(Request $request, Response $response): Response
{
$mainDomain = 'lxdapp.local';
$mainDomain = $_ENV['MAIN_DOMAIN'] ?? 'lxdapp.local';
$origin = $request->getHeaderLine('Origin');
$domain = parse_url($origin, PHP_URL_HOST); // e.g. customer1.lxdapp.local
$domain = parse_url($origin, PHP_URL_HOST); // e.g. mitul.lxdapp.local
$params = (array)$request->getParsedBody();
$configPath = __DIR__ . '/../../config.json';
@ -95,6 +98,9 @@ class ProxyController
return $this->proxyToContainer($request, $response, $ip, $name);
}
/**
* Maps a domain to a container name.
*/
private function mapDomainToContainer(string $domain): ?string
{
$configPath = __DIR__ . '/../../config.json';
@ -108,7 +114,9 @@ class ProxyController
return $config[$domain] ?? null;
}
/**
* Sends a JSON response.
*/
protected function json($response, array $data, int $status = 200)
{
$payload = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
@ -121,6 +129,9 @@ class ProxyController
->withStatus($status);
}
/**
* Generates a sanitized container name based on a subdomain.
*/
public function generateContainerName(string $subdomain): string
{
// Convert to lowercase, remove unsafe characters
@ -130,6 +141,9 @@ class ProxyController
return "container-{$sanitized}";
}
/**
* Proxies the request to the container.
*/
private function proxyToContainer(Request $request, Response $response, string $ip, string $name): Response
{
$target = $ip ? "http://$ip:80" : "http://127.0.0.1:3000";
@ -153,6 +167,9 @@ class ProxyController
}
/**
* Logs the last access to a container.
*/
protected function writeLastAccessLog(string $name, string $uri): void {
// Dynamically resolve the log directory relative to the current file
$logDir = realpath(__DIR__ . '/../../public/last-access-logs');