feat: magic for traefik redirectregex in services

This commit is contained in:
Andras Bacsai 2024-02-15 12:08:48 +01:00
parent c770c8d988
commit dfba593072

View File

@ -222,7 +222,8 @@ function fqdnLabelsForTraefik(string $uuid, Collection $domains, bool $is_force_
$basic_auth = false;
$basic_auth_middleware = null;
$redirect = false;
$redirect_middleware = null;
if ($serviceLabels) {
$basic_auth = $serviceLabels->contains(function ($value) {
return str_contains($value, 'basicauth');
@ -237,6 +238,19 @@ function fqdnLabelsForTraefik(string $uuid, Collection $domains, bool $is_force_
->filter()
->first();
}
$redirect = $serviceLabels->contains(function ($value) {
return str_contains($value, 'redirectregex');
});
if ($redirect) {
$redirect_middleware = $serviceLabels
->map(function ($item) {
if (preg_match('/traefik\.http\.middlewares\.(.*?)\.redirectregex\.regex/', $item, $matches)) {
return $matches[1];
}
})
->filter()
->first();
}
}
foreach ($domains as $loop => $domain) {
try {
@ -266,12 +280,18 @@ function fqdnLabelsForTraefik(string $uuid, Collection $domains, bool $is_force_
if ($basic_auth && $basic_auth_middleware) {
$middlewares = $middlewares . ',' . $basic_auth_middleware;
}
if ($redirect && $redirect_middleware) {
$middlewares = $middlewares . ',' . $redirect_middleware;
}
$labels->push("traefik.http.routers.{$https_label}.middlewares={$middlewares}");
} else {
$middlewares = "gzip";
if ($basic_auth && $basic_auth_middleware) {
$middlewares = $middlewares . ',' . $basic_auth_middleware;
}
if ($redirect && $redirect_middleware) {
$middlewares = $middlewares . ',' . $redirect_middleware;
}
$labels->push("traefik.http.routers.{$https_label}.middlewares={$middlewares}");
}
$labels->push("traefik.http.routers.{$https_label}.tls=true");
@ -301,12 +321,18 @@ function fqdnLabelsForTraefik(string $uuid, Collection $domains, bool $is_force_
if ($basic_auth && $basic_auth_middleware) {
$middlewares = $middlewares . ',' . $basic_auth_middleware;
}
if ($redirect && $redirect_middleware) {
$middlewares = $middlewares . ',' . $redirect_middleware;
}
$labels->push("traefik.http.routers.{$http_label}.middlewares={$middlewares}");
} else {
$middlewares = "gzip";
if ($basic_auth && $basic_auth_middleware) {
$middlewares = $middlewares . ',' . $basic_auth_middleware;
}
if ($redirect && $redirect_middleware) {
$middlewares = $middlewares . ',' . $redirect_middleware;
}
$labels->push("traefik.http.routers.{$http_label}.middlewares={$middlewares}");
}
}