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), ]; } }