{{$frontends := List .Prefix "/frontends/" }}
{{$backends :=  List .Prefix "/backends/"}}
{{$tlsconfiguration := List .Prefix "/tlsconfiguration/"}}

[backends]{{range $backends}}
{{$backend := .}}
{{$backendName := Last $backend}}
{{$servers := ListServers $backend }}

{{$circuitBreaker := Get "" . "/circuitbreaker/" "expression"}}
{{with $circuitBreaker}}
[backends."{{$backendName}}".circuitBreaker]
    expression = "{{$circuitBreaker}}"
{{end}}

{{$loadBalancer := Get "" . "/loadbalancer/" "method"}}
{{with $loadBalancer}}
[backends."{{$backendName}}".loadBalancer]
    method = "{{$loadBalancer}}"
    sticky = {{ getSticky . }}
    {{if hasStickinessLabel $backend}}
    [backends."{{$backendName}}".loadBalancer.stickiness]
      cookieName = "{{getStickinessCookieName $backend}}"
    {{end}}
{{end}}

{{$healthCheck := Get "" . "/healthcheck/" "path"}}
{{with $healthCheck}}
[backends."{{$backendName}}".healthCheck]
    path = "{{$healthCheck}}"
    interval = "{{ Get "30s" $backend "/healthcheck/" "interval" }}"
{{end}}

{{$maxConnAmt := Get "" . "/maxconn/" "amount"}}
{{$maxConnExtractorFunc := Get "" . "/maxconn/" "extractorfunc"}}
{{with $maxConnAmt}}
{{with $maxConnExtractorFunc}}
[backends."{{$backendName}}".maxConn]
    amount = {{$maxConnAmt}}
    extractorFunc = "{{$maxConnExtractorFunc}}"
{{end}}
{{end}}

{{range $servers}}
[backends."{{$backendName}}".servers."{{Last .}}"]
    url = "{{Get "" . "/url"}}"
    weight = {{Get "0"  . "/weight"}}
{{end}}
{{end}}

[frontends]{{range $frontends}}
    {{$frontend := Last .}}
    {{$entryPoints := SplitGet . "/entrypoints"}}
    [frontends."{{$frontend}}"]
    backend = "{{Get "" . "/backend"}}"
    passHostHeader = {{Get "true" . "/passHostHeader"}}
    priority = {{Get "0" . "/priority"}}
    entryPoints = [{{range $entryPoints}}
      "{{.}}",
    {{end}}]
    {{$routes := List . "/routes/"}}
        {{range $routes}}
        [frontends."{{$frontend}}".routes."{{Last .}}"]
        rule = "{{Get "" . "/rule"}}"
        {{end}}
{{end}}

{{range $tlsconfiguration}}
{{$entryPoints := SplitGet . "/entrypoints"}}
[[tlsConfiguration]]
    entryPoints = [{{range $entryPoints}}
      "{{.}}",
    {{end}}]
    [tlsConfiguration.certificate]
        certFile = """{{Get "" . "/certificate" "/certfile"}}"""
        keyFile = """{{Get "" . "/certificate" "/keyfile"}}"""
{{end}}

