163 lines
6.6 KiB
PHP
163 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Actions\Environments\AttachManagedService;
|
|
use App\Enums\EnvironmentAttachmentRole;
|
|
use App\Enums\ServiceType;
|
|
use App\Http\Requests\StoreGatewayRouteRequest;
|
|
use App\Http\Requests\UpdateGatewayRouteRequest;
|
|
use App\Models\EnvironmentAttachment;
|
|
use App\Models\Organisation;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Response;
|
|
|
|
class GatewayRouteController extends Controller
|
|
{
|
|
public function index(Request $request): Response
|
|
{
|
|
$organisation = Organisation::findOrFail($request->route('organisation'));
|
|
$application = $organisation->applications()->findOrFail($request->route('application'));
|
|
$environment = $application->environments()
|
|
->with(['attachments.service', 'attachments.serviceSlice'])
|
|
->findOrFail($request->route('environment'));
|
|
|
|
return inertia('gateway-routes/Index', [
|
|
'application' => $application,
|
|
'environment' => $environment,
|
|
'routes' => $environment->attachments
|
|
->filter(fn (EnvironmentAttachment $attachment): bool => $attachment->role === EnvironmentAttachmentRole::GATEWAY)
|
|
->values(),
|
|
]);
|
|
}
|
|
|
|
public function create(Request $request): Response
|
|
{
|
|
$organisation = Organisation::findOrFail($request->route('organisation'));
|
|
$application = $organisation->applications()->findOrFail($request->route('application'));
|
|
$environment = $application->environments()->findOrFail($request->route('environment'));
|
|
|
|
return inertia('gateway-routes/Create', [
|
|
'application' => $application,
|
|
'environment' => $environment,
|
|
'services' => $organisation->services()
|
|
->where('type', ServiceType::CADDY->value)
|
|
->orderBy('name')
|
|
->get(['id', 'name', 'type', 'category']),
|
|
]);
|
|
}
|
|
|
|
public function store(StoreGatewayRouteRequest $request): RedirectResponse
|
|
{
|
|
$organisation = Organisation::findOrFail($request->route('organisation'));
|
|
$application = $organisation->applications()->findOrFail($request->route('application'));
|
|
$environment = $application->environments()->findOrFail($request->route('environment'));
|
|
$service = $organisation->services()
|
|
->where('type', ServiceType::CADDY->value)
|
|
->findOrFail($request->integer('service_id'));
|
|
|
|
$attachment = app(AttachManagedService::class)->execute(
|
|
environment: $environment,
|
|
service: $service,
|
|
role: EnvironmentAttachmentRole::GATEWAY,
|
|
name: $request->string('name')->toString(),
|
|
isPrimary: true,
|
|
);
|
|
|
|
$attachment->serviceSlice?->update([
|
|
'config' => [
|
|
...($attachment->serviceSlice->config ?? []),
|
|
...$this->routeConfig($request),
|
|
'certificate_status' => $request->boolean('tls_enabled', true) ? 'pending' : 'disabled',
|
|
],
|
|
]);
|
|
|
|
return redirect()
|
|
->route('gateway.routes.index', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
])
|
|
->with('success', 'Gateway route created.');
|
|
}
|
|
|
|
public function edit(Request $request): Response
|
|
{
|
|
$organisation = Organisation::findOrFail($request->route('organisation'));
|
|
$application = $organisation->applications()->findOrFail($request->route('application'));
|
|
$environment = $application->environments()->findOrFail($request->route('environment'));
|
|
$route = $environment->attachments()
|
|
->with(['service', 'serviceSlice'])
|
|
->where('role', EnvironmentAttachmentRole::GATEWAY->value)
|
|
->findOrFail($request->route('route'));
|
|
|
|
return inertia('gateway-routes/Edit', [
|
|
'application' => $application,
|
|
'environment' => $environment,
|
|
'routeAttachment' => $route,
|
|
]);
|
|
}
|
|
|
|
public function update(UpdateGatewayRouteRequest $request): RedirectResponse
|
|
{
|
|
$organisation = Organisation::findOrFail($request->route('organisation'));
|
|
$application = $organisation->applications()->findOrFail($request->route('application'));
|
|
$environment = $application->environments()->findOrFail($request->route('environment'));
|
|
$route = $environment->attachments()
|
|
->with('serviceSlice')
|
|
->where('role', EnvironmentAttachmentRole::GATEWAY->value)
|
|
->findOrFail($request->route('route'));
|
|
|
|
$route->serviceSlice?->update([
|
|
'config' => [
|
|
...($route->serviceSlice->config ?? []),
|
|
...$this->routeConfig($request),
|
|
'certificate_status' => $request->filled('certificate_status')
|
|
? $request->string('certificate_status')->toString()
|
|
: ($request->boolean('tls_enabled', true) ? 'pending' : 'disabled'),
|
|
],
|
|
]);
|
|
|
|
return redirect()
|
|
->route('gateway.routes.index', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
])
|
|
->with('success', 'Gateway route updated.');
|
|
}
|
|
|
|
public function destroy(Request $request): RedirectResponse
|
|
{
|
|
$organisation = Organisation::findOrFail($request->route('organisation'));
|
|
$application = $organisation->applications()->findOrFail($request->route('application'));
|
|
$environment = $application->environments()->findOrFail($request->route('environment'));
|
|
$route = $environment->attachments()
|
|
->where('role', EnvironmentAttachmentRole::GATEWAY->value)
|
|
->findOrFail($request->route('route'));
|
|
|
|
$route->delete();
|
|
|
|
return redirect()
|
|
->route('gateway.routes.index', [
|
|
'organisation' => $organisation->id,
|
|
'application' => $application->id,
|
|
'environment' => $environment->id,
|
|
])
|
|
->with('success', 'Gateway route removed.');
|
|
}
|
|
|
|
/**
|
|
* @return array{domain: string, path_prefix: string, tls_enabled: bool}
|
|
*/
|
|
private function routeConfig(StoreGatewayRouteRequest|UpdateGatewayRouteRequest $request): array
|
|
{
|
|
return [
|
|
'domain' => $request->string('domain')->toString(),
|
|
'path_prefix' => $request->string('path_prefix')->toString(),
|
|
'tls_enabled' => $request->boolean('tls_enabled', true),
|
|
];
|
|
}
|
|
}
|