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

119 lines
4.8 KiB
PHP
Raw Normal View History

2023-04-19 10:42:15 +00:00
<?php
2023-04-25 09:01:56 +00:00
namespace App\Http\Livewire\Project\Application;
2023-04-19 10:42:15 +00:00
use App\Models\Application;
2023-05-22 09:21:03 +00:00
use App\Models\InstanceSettings;
2023-04-19 10:42:15 +00:00
use Livewire\Component;
2023-05-16 13:41:23 +00:00
use Illuminate\Support\Str;
2023-05-22 09:21:03 +00:00
use Spatie\Url\Url;
2023-04-19 10:42:15 +00: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 09:21:03 +00:00
public string|null $wildcard_domain = null;
public string|null $project_wildcard_domain = null;
public string|null $global_wildcard_domain = null;
2023-04-19 10:42:15 +00:00
2023-04-25 13:48:45 +00:00
public bool $is_static;
2023-04-19 12:00:31 +00:00
public bool $is_git_submodules_allowed;
public bool $is_git_lfs_allowed;
public bool $is_debug;
public bool $is_previews;
public bool $is_auto_deploy;
2023-05-23 10:52:14 +00:00
public bool $is_force_https;
2023-04-19 12:00:31 +00:00
2023-04-19 10:42:15 +00: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 07:02:50 +00:00
'application.install_command' => 'nullable',
'application.build_command' => 'nullable',
'application.start_command' => 'nullable',
2023-04-19 10:42:15 +00:00
'application.build_pack' => 'required',
2023-04-25 13:48:45 +00:00
'application.static_image' => 'required',
2023-04-19 10:42:15 +00:00
'application.base_directory' => 'required',
'application.publish_directory' => 'nullable',
2023-04-25 12:43:35 +00:00
'application.ports_exposes' => 'required',
'application.ports_mappings' => 'nullable',
2023-04-19 10:42:15 +00:00
];
2023-04-19 12:00:31 +00:00
public function instantSave()
{
2023-05-05 07:28:00 +00:00
// @TODO: find another way - if possible
2023-04-25 13:48:45 +00:00
$this->application->settings->is_static = $this->is_static;
2023-04-19 12:00:31 +00:00
$this->application->settings->is_git_submodules_allowed = $this->is_git_submodules_allowed;
$this->application->settings->is_git_lfs_allowed = $this->is_git_lfs_allowed;
$this->application->settings->is_debug = $this->is_debug;
$this->application->settings->is_previews = $this->is_previews;
$this->application->settings->is_auto_deploy = $this->is_auto_deploy;
2023-05-23 10:52:14 +00:00
$this->application->settings->is_force_https = $this->is_force_https;
2023-04-19 12:00:31 +00:00
$this->application->settings->save();
2023-04-26 11:01:09 +00:00
$this->application->refresh();
2023-05-09 07:54:43 +00:00
$this->emit('saved', 'Application settings updated!');
2023-05-22 09:21:03 +00:00
$this->checkWildCardDomain();
}
protected function checkWildCardDomain()
{
$coolify_instance_settings = InstanceSettings::get();
$this->project_wildcard_domain = data_get($this->application, 'environment.project.settings.wildcard_domain');
$this->global_wildcard_domain = data_get($coolify_instance_settings, 'wildcard_domain');
$this->wildcard_domain = $this->project_wildcard_domain ?? $this->global_wildcard_domain ?? null;
2023-04-19 12:00:31 +00:00
}
2023-04-19 10:42:15 +00:00
public function mount()
{
2023-04-25 13:48:45 +00:00
$this->is_static = $this->application->settings->is_static;
2023-04-19 12:00:31 +00:00
$this->is_git_submodules_allowed = $this->application->settings->is_git_submodules_allowed;
$this->is_git_lfs_allowed = $this->application->settings->is_git_lfs_allowed;
$this->is_debug = $this->application->settings->is_debug;
$this->is_previews = $this->application->settings->is_previews;
$this->is_auto_deploy = $this->application->settings->is_auto_deploy;
2023-05-23 10:52:14 +00:00
$this->is_force_https = $this->application->settings->is_force_https;
2023-05-22 09:21:03 +00: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();
}
public function generateProjectRandomDomain()
{
// Set wildcard domain based on Project wildcard domain
$url = Url::fromString($this->project_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-04-19 10:42:15 +00:00
}
public function submit()
{
2023-05-16 13:27:47 +00:00
try {
$this->validate();
2023-05-22 09:21:03 +00:00
2023-05-16 13:41:23 +00:00
$domains = Str::of($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
return Str::of($domain)->trim()->lower();
});
2023-05-22 09:21:03 +00:00
2023-05-16 13:41:23 +00:00
$this->application->fqdn = $domains->implode(',');
2023-05-16 13:27:47 +00:00
$this->application->save();
} catch (\Exception $e) {
return generalErrorHandler($e, $this);
}
2023-04-19 10:42:15 +00:00
}
}