coolify/app/Http/Livewire/Project/Application/General.php

144 lines
6.2 KiB
PHP
Raw Normal View History

2023-04-19 12:42:15 +02:00
<?php
2023-04-25 11:01:56 +02:00
namespace App\Http\Livewire\Project\Application;
2023-04-19 12:42:15 +02:00
use App\Models\Application;
2023-05-22 11:21:03 +02:00
use App\Models\InstanceSettings;
2023-04-19 12:42:15 +02:00
use Livewire\Component;
2023-05-16 15:41:23 +02:00
use Illuminate\Support\Str;
2023-05-22 11:21:03 +02:00
use Spatie\Url\Url;
2023-04-19 12:42:15 +02:00
class General extends Component
{
public string $applicationId;
public Application $application;
public string $name;
public string|null $fqdn;
public string $git_repository;
public string $git_branch;
public string|null $git_commit_sha;
public string $build_pack;
2023-05-22 11:21:03 +02:00
public string|null $wildcard_domain = null;
2023-06-22 15:25:57 +02:00
public string|null $server_wildcard_domain = null;
2023-05-22 11:21:03 +02:00
public string|null $global_wildcard_domain = null;
2023-04-19 12:42:15 +02:00
2023-04-25 15:48:45 +02:00
public bool $is_static;
2023-05-31 11:24:02 +02:00
public bool $is_git_submodules_enabled;
public bool $is_git_lfs_enabled;
public bool $is_debug_enabled;
public bool $is_preview_deployments_enabled;
public bool $is_auto_deploy_enabled;
public bool $is_force_https_enabled;
2023-04-19 14:00:31 +02:00
2023-04-19 12:42:15 +02:00
protected $rules = [
'application.name' => 'required|min:6',
'application.fqdn' => 'nullable',
'application.git_repository' => 'required',
'application.git_branch' => 'required',
'application.git_commit_sha' => 'nullable',
2023-05-05 09:02:50 +02:00
'application.install_command' => 'nullable',
'application.build_command' => 'nullable',
'application.start_command' => 'nullable',
2023-04-19 12:42:15 +02:00
'application.build_pack' => 'required',
2023-04-25 15:48:45 +02:00
'application.static_image' => 'required',
2023-04-19 12:42:15 +02:00
'application.base_directory' => 'required',
'application.publish_directory' => 'nullable',
2023-04-25 14:43:35 +02:00
'application.ports_exposes' => 'required',
'application.ports_mappings' => 'nullable',
2023-04-19 12:42:15 +02:00
];
2023-06-16 12:35:40 +02:00
protected $validationAttributes = [
'application.name' => 'name',
'application.fqdn' => 'FQDN',
'application.git_repository' => 'Git repository',
'application.git_branch' => 'Git branch',
'application.git_commit_sha' => 'Git commit SHA',
'application.install_command' => 'Install command',
'application.build_command' => 'Build command',
'application.start_command' => 'Start command',
'application.build_pack' => 'Build pack',
'application.static_image' => 'Static image',
'application.base_directory' => 'Base directory',
'application.publish_directory' => 'Publish directory',
'application.ports_exposes' => 'Ports exposes',
'application.ports_mappings' => 'Ports mappings',
];
2023-04-19 14:00:31 +02:00
public function instantSave()
{
2023-05-05 09:28:00 +02:00
// @TODO: find another way - if possible
2023-04-25 15:48:45 +02:00
$this->application->settings->is_static = $this->is_static;
if ($this->is_static) {
$this->application->ports_exposes = 80;
} else {
$this->application->ports_exposes = 3000;
}
2023-05-31 11:24:02 +02:00
$this->application->settings->is_git_submodules_enabled = $this->is_git_submodules_enabled;
$this->application->settings->is_git_lfs_enabled = $this->is_git_lfs_enabled;
$this->application->settings->is_debug_enabled = $this->is_debug_enabled;
$this->application->settings->is_preview_deployments_enabled = $this->is_preview_deployments_enabled;
$this->application->settings->is_auto_deploy_enabled = $this->is_auto_deploy_enabled;
$this->application->settings->is_force_https_enabled = $this->is_force_https_enabled;
2023-04-19 14:00:31 +02:00
$this->application->settings->save();
$this->application->save();
2023-04-26 13:01:09 +02:00
$this->application->refresh();
2023-06-15 10:48:13 +02:00
$this->emit('success', 'Application settings updated!');
2023-05-22 11:21:03 +02:00
$this->checkWildCardDomain();
}
protected function checkWildCardDomain()
{
$coolify_instance_settings = InstanceSettings::get();
2023-06-22 15:25:57 +02:00
$this->server_wildcard_domain = data_get($this->application, 'destination.server.settings.wildcard_domain');
2023-05-22 11:21:03 +02:00
$this->global_wildcard_domain = data_get($coolify_instance_settings, 'wildcard_domain');
2023-06-22 15:25:57 +02:00
$this->wildcard_domain = $this->server_wildcard_domain ?? $this->global_wildcard_domain ?? null;
2023-04-19 14:00:31 +02:00
}
2023-04-19 12:42:15 +02:00
public function mount()
{
2023-04-25 15:48:45 +02:00
$this->is_static = $this->application->settings->is_static;
2023-05-31 11:24:02 +02:00
$this->is_git_submodules_enabled = $this->application->settings->is_git_submodules_enabled;
$this->is_git_lfs_enabled = $this->application->settings->is_git_lfs_enabled;
$this->is_debug_enabled = $this->application->settings->is_debug_enabled;
$this->is_preview_deployments_enabled = $this->application->settings->is_preview_deployments_enabled;
$this->is_auto_deploy_enabled = $this->application->settings->is_auto_deploy_enabled;
$this->is_force_https_enabled = $this->application->settings->is_force_https_enabled;
2023-05-22 11:21:03 +02:00
$this->checkWildCardDomain();
}
public function generateGlobalRandomDomain()
{
// Set wildcard domain based on Global wildcard domain
$url = Url::fromString($this->global_wildcard_domain);
$host = $url->getHost();
$path = $url->getPath() === '/' ? '' : $url->getPath();
$scheme = $url->getScheme();
$this->application->fqdn = $scheme . '://' . $this->application->uuid . '.' . $host . $path;
$this->application->save();
2023-06-22 15:08:02 +02:00
$this->emit('success', 'Application settings updated!');
2023-05-22 11:21:03 +02:00
}
2023-06-22 15:25:57 +02:00
public function generateServerRandomDomain()
2023-05-22 11:21:03 +02:00
{
2023-06-22 15:25:57 +02:00
// Set wildcard domain based on Server wildcard domain
$url = Url::fromString($this->server_wildcard_domain);
2023-05-22 11:21:03 +02:00
$host = $url->getHost();
$path = $url->getPath() === '/' ? '' : $url->getPath();
$scheme = $url->getScheme();
$this->application->fqdn = $scheme . '://' . $this->application->uuid . '.' . $host . $path;
$this->application->save();
2023-06-22 15:08:02 +02:00
$this->emit('success', 'Application settings updated!');
2023-04-19 12:42:15 +02:00
}
public function submit()
{
2023-05-16 15:27:47 +02:00
try {
$this->validate();
2023-05-22 11:21:03 +02:00
2023-05-16 15:41:23 +02:00
$domains = Str::of($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
return Str::of($domain)->trim()->lower();
});
2023-05-22 11:21:03 +02:00
2023-05-16 15:41:23 +02:00
$this->application->fqdn = $domains->implode(',');
2023-05-16 15:27:47 +02:00
$this->application->save();
2023-06-22 10:04:39 +02:00
$this->emit('success', 'Application settings updated!');
2023-05-16 15:27:47 +02:00
} catch (\Exception $e) {
2023-06-09 15:55:21 +02:00
return general_error_handler(err: $e, that: $this);
2023-05-16 15:27:47 +02:00
}
2023-04-19 12:42:15 +02:00
}
}