From b576014d07e5c7d9c7af4a94f48f5e5b31fd6081 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 12 Mar 2024 10:42:56 +0100 Subject: [PATCH] fix: reload caddy issue --- app/Livewire/Server/Proxy.php | 6 +- app/Models/Server.php | 110 +++++++++++++++++- bootstrap/helpers/proxy.php | 77 ------------ .../views/livewire/server/proxy.blade.php | 10 +- 4 files changed, 113 insertions(+), 90 deletions(-) diff --git a/app/Livewire/Server/Proxy.php b/app/Livewire/Server/Proxy.php index 1e23605ff..4ccbf7d64 100644 --- a/app/Livewire/Server/Proxy.php +++ b/app/Livewire/Server/Proxy.php @@ -54,8 +54,7 @@ class Proxy extends Component SaveConfiguration::run($this->server, $this->proxy_settings); $this->server->proxy->redirect_url = $this->redirect_url; $this->server->save(); - - setup_default_redirect_404(redirect_url: $this->server->proxy->redirect_url, server: $this->server); + $this->server->setupDefault404Redirect(); $this->dispatch('success', 'Proxy configuration saved.'); } catch (\Throwable $e) { return handleError($e, $this); @@ -66,6 +65,9 @@ class Proxy extends Component { try { $this->proxy_settings = CheckConfiguration::run($this->server, true); + SaveConfiguration::run($this->server, $this->proxy_settings); + $this->server->save(); + $this->dispatch('success', 'Proxy configuration saved.'); } catch (\Throwable $e) { return handleError($e, $this); } diff --git a/app/Models/Server.php b/app/Models/Server.php index 098b19931..f067523df 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -120,6 +120,106 @@ class Server extends BaseModel } } } + public function setupDefault404Redirect() + { + $dynamic_conf_path = $this->proxyPath() . "/dynamic"; + $proxy_type = $this->proxyType(); + $redirect_url = $this->proxy->redirect_url; + + if ($proxy_type === 'TRAEFIK_V2') { + $default_redirect_file = "$dynamic_conf_path/default_redirect_404.yaml"; + } else if ($proxy_type === 'CADDY') { + $default_redirect_file = "$dynamic_conf_path/default_redirect_404.caddy"; + } + if (empty($redirect_url)) { + instant_remote_process([ + "mkdir -p $dynamic_conf_path", + "rm -f $default_redirect_file", + ], $this); + return; + } + if ($proxy_type === 'TRAEFIK_V2') { + $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' => $redirect_url, + 'permanent' => false, + ], + ], + ], + ], + ]; + $conf = Yaml::dump($dynamic_conf, 12, 2); + $conf = + "# This file is automatically generated by Coolify.\n" . + "# Do not edit it manually (only if you know what are you doing).\n\n" . + $conf; + + $base64 = base64_encode($conf); + } else if ($proxy_type === 'CADDY') { + $conf = ":80, :443 { + redir $redirect_url +}"; +ray($conf); + $conf = + "# This file is automatically generated by Coolify.\n" . + "# Do not edit it manually (only if you know what are you doing).\n\n" . + $conf; + $base64 = base64_encode($conf); + } + + + instant_remote_process([ + "mkdir -p $dynamic_conf_path", + "echo '$base64' | base64 -d > $default_redirect_file", + ], $this); + + if (config('app.env') == 'local') { + ray($conf); + } + if ($proxy_type === 'CADDY') { + $this->reloadCaddy(); + } + } public function setupDynamicProxyConfiguration() { $settings = InstanceSettings::get(); @@ -250,8 +350,7 @@ class Server extends BaseModel instant_remote_process([ "rm -f $file", ], $this); - // $this->reloadCaddy(); - + $this->reloadCaddy(); } else { $url = Url::fromString($settings->fqdn); $host = $url->getHost(); @@ -264,14 +363,15 @@ $schema://$host { instant_remote_process([ "echo '$base64' | base64 -d > $file", ], $this); - // $this->reloadCaddy(); + $this->reloadCaddy(); } } } } - public function reloadCaddy() { + public function reloadCaddy() + { return instant_remote_process([ - "docker exec coolify-proxy caddy reload --config /dynamic/Caddyfile", + "docker exec coolify-proxy caddy reload --config /config/caddy/Caddyfile.autosave", ], $this); } public function proxyPath() diff --git a/bootstrap/helpers/proxy.php b/bootstrap/helpers/proxy.php index 2bdffaf63..1eea1893e 100644 --- a/bootstrap/helpers/proxy.php +++ b/bootstrap/helpers/proxy.php @@ -218,80 +218,3 @@ function generate_default_proxy_configuration(Server $server) SaveConfiguration::run($server, $config); return $config; } - -function setup_default_redirect_404(string|null $redirect_url, Server $server) -{ - $traefik_dynamic_conf_path = $server->proxyPath() . "/dynamic"; - $traefik_default_redirect_file = "$traefik_dynamic_conf_path/default_redirect_404.yaml"; - if (empty($redirect_url)) { - instant_remote_process([ - "mkdir -p $traefik_dynamic_conf_path", - "rm -f $traefik_default_redirect_file", - ], $server); - } else { - $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' => $redirect_url, - 'permanent' => false, - ], - ], - ], - ], - ]; - $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); - instant_remote_process([ - "mkdir -p $traefik_dynamic_conf_path", - "echo '$base64' | base64 -d > $traefik_default_redirect_file", - ], $server); - - if (config('app.env') == 'local') { - ray($yaml); - } - } -} diff --git a/resources/views/livewire/server/proxy.blade.php b/resources/views/livewire/server/proxy.blade.php index ba3909739..60828ec3d 100644 --- a/resources/views/livewire/server/proxy.blade.php +++ b/resources/views/livewire/server/proxy.blade.php @@ -13,7 +13,8 @@ Save -
Before switching proxies, please read this.
+
Before switching proxies, please read this.
@if ($server->proxyType() === 'TRAEFIK_V2')
Traefik v2
@elseif ($server->proxyType() === 'CADDY') @@ -26,11 +27,8 @@ configurations. @endif - @if ($server->proxyType() === 'TRAEFIK_V2') - - @endif +