2024-01-07 15:23:41 +00:00
< ? php
namespace App\Livewire\Settings ;
use App\Models\InstanceSettings ;
2024-08-06 12:36:56 +00:00
use App\Models\Server ;
2024-12-03 23:30:39 +00:00
use Livewire\Attributes\Computed ;
2024-11-05 08:36:40 +00:00
use Livewire\Attributes\Validate ;
2024-01-07 15:23:41 +00:00
use Livewire\Component ;
class Index extends Component
{
public InstanceSettings $settings ;
2024-06-10 20:43:34 +00:00
2025-06-30 06:50:17 +00:00
public Server $server ;
2024-08-06 12:36:56 +00:00
2024-11-05 08:36:40 +00:00
#[Validate('nullable|string|max:255')]
2024-10-28 09:10:27 +00:00
public ? string $fqdn = null ;
2024-08-06 12:36:56 +00:00
2024-11-05 08:36:40 +00:00
#[Validate('required|integer|min:1025|max:65535')]
2024-10-28 09:10:27 +00:00
public int $public_port_min ;
2024-11-05 08:36:40 +00:00
#[Validate('required|integer|min:1025|max:65535')]
2024-10-28 09:10:27 +00:00
public int $public_port_max ;
2024-11-05 08:36:40 +00:00
#[Validate('nullable|string|max:255')]
2024-10-28 09:10:27 +00:00
public ? string $instance_name = null ;
2024-08-06 12:36:56 +00:00
2024-11-05 08:36:40 +00:00
#[Validate('nullable|string')]
2024-10-28 09:10:27 +00:00
public ? string $public_ipv4 = null ;
2024-11-05 08:36:40 +00:00
#[Validate('nullable|string')]
2024-10-28 09:10:27 +00:00
public ? string $public_ipv6 = null ;
2024-11-05 08:36:40 +00:00
#[Validate('required|string|timezone')]
2024-10-28 09:10:27 +00:00
public string $instance_timezone ;
2024-10-17 10:07:35 +00:00
2024-10-28 09:10:27 +00:00
public function render ()
{
return view ( 'livewire.settings.index' );
}
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public function mount ()
{
2024-10-28 09:10:27 +00:00
if ( ! isInstanceAdmin ()) {
return redirect () -> route ( 'dashboard' );
2025-01-07 14:31:43 +00:00
} else {
$this -> settings = instanceSettings ();
2025-06-30 06:50:17 +00:00
$this -> server = Server :: findOrFail ( 0 );
2025-01-07 14:31:43 +00:00
$this -> fqdn = $this -> settings -> fqdn ;
$this -> public_port_min = $this -> settings -> public_port_min ;
$this -> public_port_max = $this -> settings -> public_port_max ;
$this -> instance_name = $this -> settings -> instance_name ;
$this -> public_ipv4 = $this -> settings -> public_ipv4 ;
$this -> public_ipv6 = $this -> settings -> public_ipv6 ;
$this -> instance_timezone = $this -> settings -> instance_timezone ;
2024-01-07 15:23:41 +00:00
}
}
2024-06-10 20:43:34 +00:00
2024-12-03 23:30:39 +00:00
#[Computed]
public function timezones () : array
{
return collect ( timezone_identifiers_list ())
-> sort ()
-> values ()
-> toArray ();
}
2024-10-28 09:10:27 +00:00
public function instantSave ( $isSave = true )
2024-08-06 12:36:56 +00:00
{
2024-11-25 16:56:19 +00:00
$this -> validate ();
2024-10-28 09:10:27 +00:00
$this -> settings -> fqdn = $this -> fqdn ;
$this -> settings -> public_port_min = $this -> public_port_min ;
$this -> settings -> public_port_max = $this -> public_port_max ;
$this -> settings -> instance_name = $this -> instance_name ;
$this -> settings -> public_ipv4 = $this -> public_ipv4 ;
$this -> settings -> public_ipv6 = $this -> public_ipv6 ;
$this -> settings -> instance_timezone = $this -> instance_timezone ;
if ( $isSave ) {
$this -> settings -> save ();
$this -> dispatch ( 'success' , 'Settings updated!' );
}
2024-08-06 12:36:56 +00:00
}
public function submit ()
{
try {
$error_show = false ;
$this -> resetErrorBag ();
2024-11-14 09:02:37 +00:00
if ( ! validate_timezone ( $this -> instance_timezone )) {
$this -> instance_timezone = config ( 'app.timezone' );
2025-01-07 14:31:43 +00:00
throw new \Exception ( 'Invalid timezone.' );
} else {
$this -> settings -> instance_timezone = $this -> instance_timezone ;
2024-11-14 09:02:37 +00:00
}
2024-08-06 12:36:56 +00:00
if ( $this -> settings -> public_port_min > $this -> settings -> public_port_max ) {
$this -> addError ( 'settings.public_port_min' , 'The minimum port must be lower than the maximum port.' );
2025-01-07 14:31:43 +00:00
return ;
2024-08-06 12:36:56 +00:00
}
$this -> validate ();
2025-06-30 06:50:17 +00:00
if ( $this -> settings -> is_dns_validation_enabled && $this -> fqdn ) {
if ( ! validate_dns_entry ( $this -> fqdn , $this -> server )) {
$this -> dispatch ( 'error' , " Validating DNS failed.<br><br>Make sure you have added the DNS records correctly.<br><br> { $this -> fqdn } -> { $this -> server -> ip } <br><br>Check this <a target='_blank' class='underline dark:text-white' href='https://coolify.io/docs/knowledge-base/dns-configuration'>documentation</a> for further help. " );
2025-01-07 14:31:43 +00:00
$error_show = true ;
}
2024-08-06 12:36:56 +00:00
}
2025-06-30 06:50:17 +00:00
if ( $this -> fqdn ) {
check_domain_usage ( domain : $this -> fqdn );
2024-08-06 12:36:56 +00:00
}
2024-10-28 09:10:27 +00:00
$this -> instantSave ( isSave : false );
2024-08-06 12:36:56 +00:00
$this -> settings -> save ();
$this -> server -> setupDynamicProxyConfiguration ();
if ( ! $error_show ) {
$this -> dispatch ( 'success' , 'Instance settings updated successfully!' );
}
2025-01-07 14:31:43 +00:00
} catch ( \Exception $e ) {
2024-08-06 12:36:56 +00:00
return handleError ( $e , $this );
}
}
2024-08-26 13:26:08 +00:00
}