From 7a21312daf04522b6a1c2d0d1b7b6461b77b5e17 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 7 Mar 2024 11:14:03 +0100 Subject: [PATCH] feat: domains api endpoint --- app/Http/Controllers/Api/Domains.php | 55 ++++++++++++++++++++++++++++ app/Models/Project.php | 4 ++ routes/api.php | 2 + 3 files changed, 61 insertions(+) create mode 100644 app/Http/Controllers/Api/Domains.php diff --git a/app/Http/Controllers/Api/Domains.php b/app/Http/Controllers/Api/Domains.php new file mode 100644 index 000000000..080f5aa4f --- /dev/null +++ b/app/Http/Controllers/Api/Domains.php @@ -0,0 +1,55 @@ +json(['error' => 'Invalid token.', 'docs' => 'https://coolify.io/docs/api/authentication'], 400); + } + $projects = ModelsProject::where('team_id', $teamId)->get(); + $domains = collect(); + $applications = $projects->pluck('applications')->flatten(); + 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('/', ''); + }); + $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('/', ''); + }); + $domains->push([ + 'domain' => $fqdn, + 'ip' => $ip, + ]); + } + } + } + } + $domains = $domains->groupBy('ip')->map(function ($domain) { + return $domain->pluck('domain')->flatten(); + }); + + return response()->json($domains); + } +} diff --git a/app/Models/Project.php b/app/Models/Project.php index b9afc7426..6416971f6 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -45,6 +45,10 @@ public function team() return $this->belongsTo(Team::class); } + public function services() + { + return $this->hasManyThrough(Service::class, Environment::class); + } public function applications() { return $this->hasManyThrough(Application::class, Environment::class); diff --git a/routes/api.php b/routes/api.php index 4ef5500f8..a1ddd666e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,6 +1,7 @@