feat: domains api endpoint

This commit is contained in:
Andras Bacsai 2024-03-07 11:14:03 +01:00
parent 051a1405e7
commit 7a21312daf
3 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,55 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Project as ModelsProject;
use Illuminate\Http\Request;
class Domains extends Controller
{
public function domains(Request $request)
{
$teamId = get_team_id_from_token();
if (is_null($teamId)) {
return response()->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);
}
}

View File

@ -45,6 +45,10 @@ public function team()
return $this->belongsTo(Team::class); return $this->belongsTo(Team::class);
} }
public function services()
{
return $this->hasManyThrough(Service::class, Environment::class);
}
public function applications() public function applications()
{ {
return $this->hasManyThrough(Application::class, Environment::class); return $this->hasManyThrough(Application::class, Environment::class);

View File

@ -1,6 +1,7 @@
<?php <?php
use App\Http\Controllers\Api\Deploy; use App\Http\Controllers\Api\Deploy;
use App\Http\Controllers\Api\Domains;
use App\Http\Controllers\Api\Project; use App\Http\Controllers\Api\Project;
use App\Http\Controllers\Api\Server; use App\Http\Controllers\Api\Server;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -34,6 +35,7 @@
Route::get('/projects', [Project::class, 'projects']); Route::get('/projects', [Project::class, 'projects']);
Route::get('/project/{uuid}', [Project::class, 'project_by_uuid']); Route::get('/project/{uuid}', [Project::class, 'project_by_uuid']);
Route::get('/project/{uuid}/{environment_name}', [Project::class, 'environment_details']); Route::get('/project/{uuid}/{environment_name}', [Project::class, 'environment_details']);
Route::get('/domains', [Domains::class, 'domains']);
}); });
Route::get('/{any}', function () { Route::get('/{any}', function () {