39 lines
1.1 KiB
PHP
39 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Models\EnvironmentAttachment;
|
|
|
|
class CaddyRouteRenderer
|
|
{
|
|
/**
|
|
* @param array<int, string> $upstreams
|
|
*/
|
|
public function render(EnvironmentAttachment $attachment, array $upstreams): string
|
|
{
|
|
$config = $attachment->serviceSlice?->config ?? [];
|
|
$domain = $config['domain'] ?? $attachment->serviceSlice?->name ?? $attachment->environment->name;
|
|
$pathPrefix = $config['path_prefix'] ?? '/';
|
|
$siteAddress = ($config['tls_enabled'] ?? true) ? $domain : "http://{$domain}";
|
|
$upstreamTargets = $upstreams === [] ? ['web:80'] : $upstreams;
|
|
|
|
if ($pathPrefix === '/') {
|
|
return implode("\n", [
|
|
"{$siteAddress} {",
|
|
' reverse_proxy '.implode(' ', $upstreamTargets),
|
|
'}',
|
|
]);
|
|
}
|
|
|
|
$normalizedPath = rtrim($pathPrefix, '/');
|
|
|
|
return implode("\n", [
|
|
"{$siteAddress} {",
|
|
" handle_path {$normalizedPath}* {",
|
|
' reverse_proxy '.implode(' ', $upstreamTargets),
|
|
' }',
|
|
'}',
|
|
]);
|
|
}
|
|
}
|