lasthourcloud/app/Http/Livewire/Settings/Form.php

205 lines
7.0 KiB
PHP
Raw Normal View History

2023-04-25 08:06:45 +00:00
<?php
2023-04-25 08:47:13 +00:00
namespace App\Http\Livewire\Settings;
2023-04-25 08:06:45 +00:00
use App\Models\InstanceSettings as ModelsInstanceSettings;
2023-05-25 10:00:09 +00:00
use App\Models\Server;
2023-04-25 08:06:45 +00:00
use Livewire\Component;
2023-05-25 10:00:09 +00:00
use Spatie\Url\Url;
use Symfony\Component\Yaml\Yaml;
2023-04-25 08:06:45 +00:00
2023-04-25 08:47:13 +00:00
class Form extends Component
2023-04-25 08:06:45 +00:00
{
public ModelsInstanceSettings $settings;
public $do_not_track;
public $is_auto_update_enabled;
public $is_registration_enabled;
2023-05-31 07:22:08 +00:00
protected string $dynamic_config_path;
protected Server $server;
2023-04-25 08:06:45 +00:00
protected $rules = [
'settings.fqdn' => 'nullable',
'settings.wildcard_domain' => 'nullable',
'settings.public_port_min' => 'required',
'settings.public_port_max' => 'required',
2023-05-31 07:22:08 +00:00
'settings.default_redirect_404' => 'nullable',
2023-04-25 08:06:45 +00:00
];
public function mount()
{
$this->do_not_track = $this->settings->do_not_track;
$this->is_auto_update_enabled = $this->settings->is_auto_update_enabled;
$this->is_registration_enabled = $this->settings->is_registration_enabled;
}
public function instantSave()
{
$this->settings->do_not_track = $this->do_not_track;
$this->settings->is_auto_update_enabled = $this->is_auto_update_enabled;
$this->settings->is_registration_enabled = $this->is_registration_enabled;
$this->settings->save();
2023-05-09 07:54:43 +00:00
$this->emit('saved', 'Settings updated!');
2023-04-25 08:06:45 +00:00
}
2023-05-31 07:22:08 +00:00
private function setup_instance_fqdn()
2023-04-25 08:06:45 +00:00
{
2023-05-31 07:22:08 +00:00
$file = "$this->dynamic_config_path/coolify.yaml";
2023-05-25 10:04:43 +00:00
if (empty($this->settings->fqdn)) {
remote_process([
2023-05-31 07:22:08 +00:00
"rm -f $file",
], $this->server);
2023-05-25 10:04:43 +00:00
} else {
2023-05-25 10:00:09 +00:00
$url = Url::fromString($this->settings->fqdn);
$host = $url->getHost();
$schema = $url->getScheme();
$traefik_dynamic_conf = [
'http' =>
[
'routers' =>
[
2023-05-26 09:01:50 +00:00
'coolify-http' =>
2023-05-25 10:00:09 +00:00
[
2023-05-26 09:01:50 +00:00
'entryPoints' => [
0 => 'http',
],
2023-05-25 10:00:09 +00:00
'service' => 'coolify',
'rule' => "Host(`{$host}`)",
],
],
'services' =>
[
'coolify' =>
[
'loadBalancer' =>
[
'servers' =>
[
0 =>
[
'url' => 'http://coolify:80',
],
],
],
],
],
],
];
2023-05-26 09:01:50 +00:00
2023-05-26 08:54:22 +00:00
if ($schema === 'https') {
2023-05-26 09:01:50 +00:00
$traefik_dynamic_conf['http']['routers']['coolify-http']['middlewares'] = [
2023-05-26 08:54:22 +00:00
0 => 'redirect-to-https@docker',
];
2023-05-26 09:01:50 +00:00
$traefik_dynamic_conf['http']['routers']['coolify-https'] = [
'entryPoints' => [
0 => 'https',
],
'service' => 'coolify',
'rule' => "Host(`{$host}`)",
'tls' => [
'certresolver' => 'letsencrypt',
],
2023-05-26 08:54:22 +00:00
];
}
2023-05-31 07:22:08 +00:00
$this->save_configuration_to_disk($traefik_dynamic_conf, $file);
}
}
private function setup_default_redirect_404()
{
$file = "$this->dynamic_config_path/default_redirect_404.yaml";
if (empty($this->settings->default_redirect_404)) {
2023-05-25 10:00:09 +00:00
remote_process([
2023-05-31 07:22:08 +00:00
"rm -f $file",
], $this->server);
} else {
$url = Url::fromString($this->settings->default_redirect_404);
$host = $url->getHost();
$schema = $url->getScheme();
$traefik_dynamic_conf = [
'http' =>
[
'routers' =>
[
'catchall' =>
[
'entryPoints' => [
0 => 'http',
1 => 'https',
],
'service' => 'noop',
'rule' => "HostRegexp(`{catchall:.*}`)",
'priority' => 1,
'middlewares' => [
0 => 'redirect-regexp@file',
],
],
],
'services' =>
[
'noop' =>
[
'loadBalancer' =>
[
'servers' =>
[
0 =>
[
'url' => '',
],
],
],
],
],
'middlewares' =>
[
'redirect-regexp' =>
[
'redirectRegex' =>
[
'regex' => '(.*)',
'replacement' => $this->settings->default_redirect_404,
'permanent' => false,
],
],
],
],
];
$this->save_configuration_to_disk($traefik_dynamic_conf, $file);
}
}
private function save_configuration_to_disk(array $traefik_dynamic_conf, string $file)
{
$yaml = Yaml::dump($traefik_dynamic_conf, 12, 2);
$yaml =
"# This file is automatically generated by Coolify.\n" .
"# Do not edit it manually (only if you know what are you doing).\n\n" .
$yaml;
$base64 = base64_encode($yaml);
remote_process([
"mkdir -p $this->dynamic_config_path",
"echo '$base64' | base64 -d > $file",
], $this->server);
if (config('app.env') == 'local') {
ray($yaml);
}
}
public function submit()
{
$this->resetErrorBag();
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.');
return;
}
$this->validate();
$this->settings->save();
$this->dynamic_config_path = '/data/coolify/proxy/dynamic';
if (config('app.env') == 'local') {
$this->server = Server::findOrFail(1);
} else {
$this->server = Server::findOrFail(0);
2023-05-25 10:00:09 +00:00
}
2023-05-31 07:22:08 +00:00
$this->setup_instance_fqdn();
$this->setup_default_redirect_404();
2023-04-25 08:06:45 +00:00
}
}