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

123 lines
4.1 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;
protected $rules = [
'settings.fqdn' => 'nullable',
'settings.wildcard_domain' => 'nullable',
'settings.public_port_min' => 'required',
'settings.public_port_max' => 'required',
];
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
}
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;
}
2023-04-25 08:47:13 +00:00
$this->validate();
2023-04-25 08:06:45 +00:00
$this->settings->save();
2023-05-25 10:04:43 +00:00
2023-05-26 08:06:01 +00:00
$dynamic_config_path = '/data/coolify/proxy/dynamic';
2023-05-25 10:04:43 +00:00
if (config('app.env') == 'local') {
$server = Server::findOrFail(1);
} else {
$server = Server::findOrFail(0);
}
if (empty($this->settings->fqdn)) {
remote_process([
"rm -f $dynamic_config_path/coolify.yaml",
2023-05-25 13:48:26 +00:00
], $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-26 09:01:50 +00:00
$yaml = Yaml::dump($traefik_dynamic_conf, 12, 2);
2023-05-26 08:28:38 +00:00
if (config('app.env') == 'local') {
2023-05-26 10:04:44 +00:00
// dump($yaml);
2023-05-26 08:28:38 +00:00
}
2023-05-25 10:00:09 +00:00
$base64 = base64_encode($yaml);
remote_process([
"mkdir -p $dynamic_config_path",
"echo '$base64' | base64 -d > $dynamic_config_path/coolify.yaml",
2023-05-25 13:48:26 +00:00
], $server);
2023-05-25 10:00:09 +00:00
}
2023-04-25 08:06:45 +00:00
}
}