Modules/businessdev.ALbuild.Containers/Private/Get-BcTraefikConfig.ps1

function Get-BcTraefikConfig {
    <#
    .SYNOPSIS
        Builds a Traefik v3 file-provider dynamic configuration object for a container.
    .DESCRIPTION
        Internal, pure helper (no I/O) so routing construction is unit-testable. Supports two
        routing modes:
          Subdomain -> Host(`<name>.<domain>`)
          PathPrefix -> Host(`<domain>`) && PathPrefix(`/<name>`) (with a strip-prefix middleware)
        Returns a hashtable mirroring Traefik's dynamic config schema (http.routers/services/
        middlewares).
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory)] [string] $ContainerName,
        [Parameter(Mandatory)] [string] $Domain,
        [Parameter(Mandatory)] [ValidateSet('Subdomain', 'PathPrefix')] [string] $Routing,
        [Parameter(Mandatory)] [string] $BackendHost,
        [int] $Port = 80,
        [string] $Scheme = 'http',
        [string] $EntryPoint = 'websecure',
        [string] $CertResolver = 'letsencrypt'
    )

    $routerName  = "$ContainerName-web"
    $serviceName = "$ContainerName-web"

    if ($Routing -eq 'Subdomain') {
        $rule = "Host(``$ContainerName.$Domain``)"
    }
    else {
        $rule = "Host(``$Domain``) && PathPrefix(``/$ContainerName``)"
    }

    $router = [ordered]@{
        rule        = $rule
        entryPoints = @($EntryPoint)
        service     = $serviceName
        tls         = @{ certResolver = $CertResolver }
    }

    $middlewares = [ordered]@{}
    if ($Routing -eq 'PathPrefix') {
        $mwName = "$ContainerName-stripprefix"
        $middlewares[$mwName] = @{ stripPrefix = @{ prefixes = @("/$ContainerName") } }
        $router['middlewares'] = @($mwName)
    }

    $service = @{
        loadBalancer = @{ servers = @(@{ url = "$($Scheme)://$($BackendHost):$Port" }) }
    }

    return @{
        http = [ordered]@{
            routers     = [ordered]@{ $routerName = $router }
            services    = [ordered]@{ $serviceName = $service }
            middlewares = $middlewares
        }
    }
}