refactor: improve proxy configuration and code consistency in Server model

- Standardized spacing in conditional statements for better readability.
- Updated proxy redirect handling to ensure default values are set correctly.
- Modified dynamic configuration path concatenation for clarity.
- Changed router rule to 'PathPrefix' and adjusted priority for better routing behavior.
- Replaced empty check with 'filled' helper for improved code clarity.
This commit is contained in:
Andras Bacsai
2024-12-06 13:08:04 +01:00
parent f82d95e908
commit 659309c5b4

View File

@@ -105,12 +105,12 @@ class Server extends BaseModel
]);
}
}
if (!isset($server->proxy->redirect_enabled)) {
if (! isset($server->proxy->redirect_enabled)) {
$server->proxy->redirect_enabled = true;
}
});
static::retrieved(function ($server) {
if (!isset($server->proxy->redirect_enabled)) {
if (! isset($server->proxy->redirect_enabled)) {
$server->proxy->redirect_enabled = true;
}
});
@@ -197,7 +197,7 @@ class Server extends BaseModel
$banner =
"# This file is generated by Coolify, do not edit it manually.\n".
"# Disable the default redirect to customize (only if you know what are you doing).\n\n";
$dynamic_conf_path = $this->proxyPath() . '/dynamic';
$dynamic_conf_path = $this->proxyPath().'/dynamic';
$proxy_type = $this->proxyType();
$redirect_enabled = $this->proxy->redirect_enabled ?? true;
$redirect_url = $this->proxy->redirect_url;
@@ -214,7 +214,7 @@ class Server extends BaseModel
"rm -f $dynamic_conf_path/default_redirect_404.caddy",
], $this);
if (!$redirect_enabled) {
if (! $redirect_enabled) {
instant_remote_process(["rm -f $default_redirect_file"], $this);
} else {
if ($proxy_type === ProxyTypes::CADDY->value) {
@@ -237,11 +237,8 @@ class Server extends BaseModel
1 => 'https',
],
'service' => 'noop',
'rule' => 'HostRegexp(`.+`)',
'tls' => [
'certResolver' => 'letsencrypt',
],
'priority' => 1,
'rule' => 'PathPrefix(`/`)',
'priority' => -1000,
],
],
'services' => [
@@ -253,10 +250,11 @@ class Server extends BaseModel
],
],
];
if (!empty($redirect_url)) {
if (filled($redirect_url)) {
$dynamic_conf['http']['routers']['catchall']['middlewares'] = [
0 => 'redirect-regexp',
];
$dynamic_conf['http']['services']['noop']['loadBalancer']['servers'][0] = [
'url' => '',
];
@@ -607,6 +605,7 @@ $schema://$host {
$memory = json_decode($memory, true);
$parsedCollection = collect($memory)->map(function ($metric) {
$usedPercent = $metric['usedPercent'] ?? 0.0;
return [(int) $metric['time'], (float) $usedPercent];
});