catch-all traefik

This commit is contained in:
Andras Bacsai
2023-05-31 09:22:08 +02:00
parent 3d4e985aa6
commit 40bab90946
11 changed files with 164 additions and 46 deletions

View File

@@ -14,12 +14,15 @@ class Form extends Component
public $do_not_track;
public $is_auto_update_enabled;
public $is_registration_enabled;
protected string $dynamic_config_path;
protected Server $server;
protected $rules = [
'settings.fqdn' => 'nullable',
'settings.wildcard_domain' => 'nullable',
'settings.public_port_min' => 'required',
'settings.public_port_max' => 'required',
'settings.default_redirect_404' => 'nullable',
];
public function mount()
{
@@ -35,27 +38,13 @@ class Form extends Component
$this->settings->save();
$this->emit('saved', 'Settings updated!');
}
public function submit()
private function setup_instance_fqdn()
{
$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();
$dynamic_config_path = '/data/coolify/proxy/dynamic';
if (config('app.env') == 'local') {
$server = Server::findOrFail(1);
} else {
$server = Server::findOrFail(0);
}
$file = "$this->dynamic_config_path/coolify.yaml";
if (empty($this->settings->fqdn)) {
remote_process([
"rm -f $dynamic_config_path/coolify.yaml",
], $server);
"rm -f $file",
], $this->server);
} else {
$url = Url::fromString($this->settings->fqdn);
$host = $url->getHost();
@@ -108,19 +97,108 @@ class Form extends Component
],
];
}
$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;
if (config('app.env') == 'local') {
dump($yaml);
}
$base64 = base64_encode($yaml);
remote_process([
"mkdir -p $dynamic_config_path",
"echo '$base64' | base64 -d > $dynamic_config_path/coolify.yaml",
], $server);
$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)) {
remote_process([
"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);
}
$this->setup_instance_fqdn();
$this->setup_default_redirect_404();
}
}