From 34508a2fd15fa3362a43335ac4b29acbc03075a4 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 21 Jun 2024 21:35:02 +0200 Subject: [PATCH] feat: Add API endpoint to update application by UUID --- app/Http/Controllers/Api/Applications.php | 51 +++++++- app/Http/Controllers/Api/Domains.php | 150 ---------------------- app/Http/Controllers/Api/Server.php | 105 +++++++++++++++ routes/api.php | 7 +- 4 files changed, 157 insertions(+), 156 deletions(-) diff --git a/app/Http/Controllers/Api/Applications.php b/app/Http/Controllers/Api/Applications.php index b4831cb57..a638adecd 100644 --- a/app/Http/Controllers/Api/Applications.php +++ b/app/Http/Controllers/Api/Applications.php @@ -43,6 +43,46 @@ public function application_by_uuid(Request $request) return response()->json($application); } + public function update_by_uuid(Request $request) + { + $teamId = get_team_id_from_token(); + if (is_null($teamId)) { + return invalid_token(); + } + + if ($request->collect()->count() == 0) { + return response()->json([ + 'message' => 'No data provided.', + ], 400); + } + $application = Application::where('uuid', $request->uuid)->first(); + + if (! $application) { + return response()->json([ + 'success' => false, + 'message' => 'Application not found', + ], 404); + } + ray($request->collect()); + + // if ($request->has('domains')) { + // $existingDomains = explode(',', $application->fqdn); + // $newDomains = $request->domains; + // $filteredNewDomains = array_filter($newDomains, function ($domain) use ($existingDomains) { + // return ! in_array($domain, $existingDomains); + // }); + // $mergedDomains = array_unique(array_merge($existingDomains, $filteredNewDomains)); + // $application->fqdn = implode(',', $mergedDomains); + // $application->custom_labels = base64_encode(implode("\n ", generateLabelsApplication($application))); + // $application->save(); + // } + + return response()->json([ + 'message' => 'Application updated successfully.', + 'application' => serialize_api_response($application), + ]); + } + public function action_deploy(Request $request) { $teamId = get_team_id_from_token(); @@ -87,6 +127,7 @@ public function action_stop(Request $request) return invalid_token(); } $uuid = $request->route('uuid'); + $sync = $request->query->get('sync') ?? false; if (! $uuid) { return response()->json(['error' => 'UUID is required.'], 400); } @@ -94,9 +135,15 @@ public function action_stop(Request $request) if (! $application) { return response()->json(['error' => 'Application not found.'], 404); } - StopApplication::dispatch($application); + if ($sync) { + StopApplication::run($application); - return response()->json(['message' => 'Stopping request queued.'], 200); + return response()->json(['message' => 'Stopped the application.'], 200); + } else { + StopApplication::dispatch($application); + + return response()->json(['message' => 'Stopping request queued.'], 200); + } } public function action_restart(Request $request) diff --git a/app/Http/Controllers/Api/Domains.php b/app/Http/Controllers/Api/Domains.php index 86ff0cccc..6473b64de 100644 --- a/app/Http/Controllers/Api/Domains.php +++ b/app/Http/Controllers/Api/Domains.php @@ -4,161 +4,11 @@ use App\Http\Controllers\Controller; use App\Models\Application; -use App\Models\InstanceSettings; -use App\Models\Project as ModelsProject; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class Domains extends Controller { - public function domains(Request $request) - { - $teamId = get_team_id_from_token(); - if (is_null($teamId)) { - return invalid_token(); - } - $uuid = $request->query->get('uuid'); - if ($uuid) { - $domains = Application::getDomainsByUuid($uuid); - - return response()->json([ - 'uuid' => $uuid, - 'domains' => $domains, - ]); - } - $projects = ModelsProject::where('team_id', $teamId)->get(); - $domains = collect(); - $applications = $projects->pluck('applications')->flatten(); - $settings = InstanceSettings::get(); - if ($applications->count() > 0) { - foreach ($applications as $application) { - $ip = $application->destination->server->ip; - $fqdn = str($application->fqdn)->explode(',')->map(function ($fqdn) { - return str($fqdn)->replace('http://', '')->replace('https://', '')->replace('/', ''); - }); - if ($ip === 'host.docker.internal') { - if ($settings->public_ipv4) { - $domains->push([ - 'domain' => $fqdn, - 'ip' => $settings->public_ipv4, - ]); - } - if ($settings->public_ipv6) { - $domains->push([ - 'domain' => $fqdn, - 'ip' => $settings->public_ipv6, - ]); - } - if (! $settings->public_ipv4 && ! $settings->public_ipv6) { - $domains->push([ - 'domain' => $fqdn, - 'ip' => $ip, - ]); - } - } else { - $domains->push([ - 'domain' => $fqdn, - 'ip' => $ip, - ]); - } - } - } - $services = $projects->pluck('services')->flatten(); - if ($services->count() > 0) { - foreach ($services as $service) { - $service_applications = $service->applications; - if ($service_applications->count() > 0) { - foreach ($service_applications as $application) { - $fqdn = str($application->fqdn)->explode(',')->map(function ($fqdn) { - return str($fqdn)->replace('http://', '')->replace('https://', '')->replace('/', ''); - }); - if ($ip === 'host.docker.internal') { - if ($settings->public_ipv4) { - $domains->push([ - 'domain' => $fqdn, - 'ip' => $settings->public_ipv4, - ]); - } - if ($settings->public_ipv6) { - $domains->push([ - 'domain' => $fqdn, - 'ip' => $settings->public_ipv6, - ]); - } - if (! $settings->public_ipv4 && ! $settings->public_ipv6) { - $domains->push([ - 'domain' => $fqdn, - 'ip' => $ip, - ]); - } - } else { - $domains->push([ - 'domain' => $fqdn, - 'ip' => $ip, - ]); - } - } - } - } - } - $domains = $domains->groupBy('ip')->map(function ($domain) { - return $domain->pluck('domain')->flatten(); - })->map(function ($domain, $ip) { - return [ - 'ip' => $ip, - 'domains' => $domain, - ]; - })->values(); - - return response()->json($domains); - } - - public function updateDomains(Request $request) - { - $teamId = get_team_id_from_token(); - if (is_null($teamId)) { - return invalid_token(); - } - $validator = Validator::make($request->all(), [ - 'uuid' => 'required|string|exists:applications,uuid', - 'domains' => 'required|array', - 'domains.*' => 'required|string|distinct', - ]); - - if ($validator->fails()) { - return response()->json([ - 'success' => false, - 'message' => 'Validation failed', - 'errors' => $validator->errors(), - ], 422); - } - - $application = Application::where('uuid', $request->uuid)->first(); - - if (! $application) { - return response()->json([ - 'success' => false, - 'message' => 'Application not found', - ], 404); - } - - $existingDomains = explode(',', $application->fqdn); - $newDomains = $request->domains; - $filteredNewDomains = array_filter($newDomains, function ($domain) use ($existingDomains) { - return ! in_array($domain, $existingDomains); - }); - $mergedDomains = array_unique(array_merge($existingDomains, $filteredNewDomains)); - $application->fqdn = implode(',', $mergedDomains); - $application->custom_labels = base64_encode(implode("\n ", generateLabelsApplication($application))); - $application->save(); - - return response()->json([ - 'success' => true, - 'message' => 'Domains updated successfully', - 'application' => $application, - ]); - } - public function deleteDomains(Request $request) { $teamId = get_team_id_from_token(); diff --git a/app/Http/Controllers/Api/Server.php b/app/Http/Controllers/Api/Server.php index 9f88a3b28..7a6090bf8 100644 --- a/app/Http/Controllers/Api/Server.php +++ b/app/Http/Controllers/Api/Server.php @@ -3,6 +3,9 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; +use App\Models\Application; +use App\Models\InstanceSettings; +use App\Models\Project; use App\Models\Server as ModelsServer; use Illuminate\Http\Request; @@ -59,4 +62,106 @@ public function server_by_uuid(Request $request) return response()->json($server); } + + public function get_domains_by_server(Request $request) + { + $teamId = get_team_id_from_token(); + if (is_null($teamId)) { + return invalid_token(); + } + $uuid = $request->query->get('uuid'); + if ($uuid) { + $domains = Application::getDomainsByUuid($uuid); + + return response()->json([ + 'uuid' => $uuid, + 'domains' => $domains, + ]); + } + $projects = Project::where('team_id', $teamId)->get(); + $domains = collect(); + $applications = $projects->pluck('applications')->flatten(); + $settings = InstanceSettings::get(); + if ($applications->count() > 0) { + foreach ($applications as $application) { + $ip = $application->destination->server->ip; + $fqdn = str($application->fqdn)->explode(',')->map(function ($fqdn) { + return str($fqdn)->replace('http://', '')->replace('https://', '')->replace('/', ''); + }); + if ($ip === 'host.docker.internal') { + if ($settings->public_ipv4) { + $domains->push([ + 'domain' => $fqdn, + 'ip' => $settings->public_ipv4, + ]); + } + if ($settings->public_ipv6) { + $domains->push([ + 'domain' => $fqdn, + 'ip' => $settings->public_ipv6, + ]); + } + if (! $settings->public_ipv4 && ! $settings->public_ipv6) { + $domains->push([ + 'domain' => $fqdn, + 'ip' => $ip, + ]); + } + } else { + $domains->push([ + 'domain' => $fqdn, + 'ip' => $ip, + ]); + } + } + } + $services = $projects->pluck('services')->flatten(); + if ($services->count() > 0) { + foreach ($services as $service) { + $service_applications = $service->applications; + if ($service_applications->count() > 0) { + foreach ($service_applications as $application) { + $fqdn = str($application->fqdn)->explode(',')->map(function ($fqdn) { + return str($fqdn)->replace('http://', '')->replace('https://', '')->replace('/', ''); + }); + if ($ip === 'host.docker.internal') { + if ($settings->public_ipv4) { + $domains->push([ + 'domain' => $fqdn, + 'ip' => $settings->public_ipv4, + ]); + } + if ($settings->public_ipv6) { + $domains->push([ + 'domain' => $fqdn, + 'ip' => $settings->public_ipv6, + ]); + } + if (! $settings->public_ipv4 && ! $settings->public_ipv6) { + $domains->push([ + 'domain' => $fqdn, + 'ip' => $ip, + ]); + } + } else { + $domains->push([ + 'domain' => $fqdn, + 'ip' => $ip, + ]); + } + } + } + } + } + $domains = $domains->groupBy('ip')->map(function ($domain) { + return $domain->pluck('domain')->flatten(); + })->map(function ($domain, $ip) { + return [ + 'ip' => $ip, + 'domains' => $domain, + ]; + })->values(); + + return response()->json($domains); + } } diff --git a/routes/api.php b/routes/api.php index bea245e24..858a2282f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -38,19 +38,18 @@ Route::get('/servers', [Server::class, 'servers']); Route::get('/server/{uuid}', [Server::class, 'server_by_uuid']); + Route::get('/servers/domains', [Server::class, 'get_domains_by_server']); Route::get('/resources', [Resources::class, 'resources']); Route::get('/applications', [Applications::class, 'applications']); Route::get('/application/{uuid}', [Applications::class, 'application_by_uuid']); - + Route::put('/application/{uuid}', [Applications::class, 'update_by_uuid']); Route::match(['get', 'post'], '/application/{uuid}/action/deploy', [Applications::class, 'action_deploy']); Route::match(['get', 'post'], '/application/{uuid}/action/restart', [Applications::class, 'action_restart']); Route::match(['get', 'post'], '/application/{uuid}/action/stop', [Applications::class, 'action_stop']); - Route::get('/domains', [Domains::class, 'domains']); - // Route::put('/domains', [Domains::class, 'updateDomains']); - // Route::delete('/domains', [Domains::class, 'deleteDomains']); + Route::delete('/domains', [Domains::class, 'deleteDomains']); Route::get('/teams', [Team::class, 'teams']); Route::get('/team/current', [Team::class, 'current_team']);