{{$backendServers := .Servers}}
[backends]
{{range $backendName, $servers := .Servers}}
{{ $backend := index $servers 0 }}

  {{ $circuitBreaker := getCircuitBreaker $backend.SegmentLabels }}
  {{if $circuitBreaker }}
  [backends."backend-{{ $backendName }}".circuitBreaker]
    expression = "{{ $circuitBreaker.Expression }}"
  {{end}}

  {{ $responseForwarding := getResponseForwarding $backend.SegmentLabels }}
  {{if $responseForwarding }}
  [backends."backend-{{ $backendName }}".responseForwarding]
    flushInterval = "{{ $responseForwarding.FlushInterval }}"
  {{end}}

  {{ $loadBalancer := getLoadBalancer $backend.SegmentLabels }}
  {{if $loadBalancer }}
    [backends."backend-{{ $backendName }}".loadBalancer]
      method = "{{ $loadBalancer.Method }}"
      {{if $loadBalancer.Stickiness }}
      [backends."backend-{{ $backendName }}".loadBalancer.stickiness]
        cookieName = "{{ $loadBalancer.Stickiness.CookieName }}"
      {{end}}
  {{end}}

  {{ $maxConn := getMaxConn $backend.SegmentLabels }}
  {{if $maxConn }}
  [backends."backend-{{ $backendName }}".maxConn]
    extractorFunc = "{{ $maxConn.ExtractorFunc }}"
    amount = {{ $maxConn.Amount }}
  {{end}}

  {{ $healthCheck := getHealthCheck $backend.SegmentLabels }}
  {{if $healthCheck }}
  [backends."backend-{{ $backendName }}".healthCheck]
    scheme = "{{ $healthCheck.Scheme }}"
    path = "{{ $healthCheck.Path }}"
    port = {{ $healthCheck.Port }}
    interval = "{{ $healthCheck.Interval }}"
    timeout = "{{ $healthCheck.Timeout }}"
    hostname = "{{ $healthCheck.Hostname }}"
    {{if $healthCheck.Headers }}
    [backends."backend-{{ $backendName }}".healthCheck.headers]
      {{range $k, $v := $healthCheck.Headers }}
      {{$k}} = "{{$v}}"
      {{end}}
    {{end}}
  {{end}}

  {{ $buffering := getBuffering $backend.SegmentLabels }}
  {{if $buffering }}
  [backends."backend-{{ $backendName }}".buffering]
    maxRequestBodyBytes = {{ $buffering.MaxRequestBodyBytes }}
    memRequestBodyBytes = {{ $buffering.MemRequestBodyBytes }}
    maxResponseBodyBytes = {{ $buffering.MaxResponseBodyBytes }}
    memResponseBodyBytes = {{ $buffering.MemResponseBodyBytes }}
    retryExpression = "{{ $buffering.RetryExpression }}"
  {{end}}

  {{range $serverName, $server := getServers $servers }}
  [backends."backend-{{ $backendName }}".servers."{{ $serverName }}"]
    url = "{{ $server.URL }}"
    weight = {{ $server.Weight }}
  {{end}}

{{end}}

[frontends]
{{range $frontendName, $containers := .Frontends }}
  {{ $container := index $containers 0 }}

  [frontends."frontend-{{ $frontendName }}"]
    backend = "backend-{{ getBackendName $container }}"
    priority = {{ getPriority $container.SegmentLabels }}
    passHostHeader = {{ getPassHostHeader $container.SegmentLabels }}
    passTLSCert = {{ getPassTLSCert $container.SegmentLabels }}

    entryPoints = [{{range getEntryPoints $container.SegmentLabels }}
      "{{.}}",
      {{end}}]

    {{ $tlsClientCert := getPassTLSClientCert $container.SegmentLabels }}
    {{if $tlsClientCert }}
    [frontends."frontend-{{ $frontendName }}".passTLSClientCert]
      pem = {{ $tlsClientCert.PEM }}
      {{ $infos := $tlsClientCert.Infos }}
      {{if $infos }}
      [frontends."frontend-{{ $frontendName }}".passTLSClientCert.infos]
        notAfter = {{ $infos.NotAfter   }}
        notBefore = {{ $infos.NotBefore }}
        sans = {{ $infos.Sans }}
        {{ $subject := $infos.Subject }}
        {{if $subject }}
        [frontends."frontend-{{ $frontendName }}".passTLSClientCert.infos.subject]
          country = {{ $subject.Country }}
          province = {{ $subject.Province }}
          locality = {{ $subject.Locality }}
          organization = {{ $subject.Organization }}
          commonName = {{ $subject.CommonName }}
          serialNumber = {{ $subject.SerialNumber }}
          domainComponent = {{ $subject.DomainComponent }}
        {{end}}
        {{ $issuer := $infos.Issuer }}
        {{if $issuer }}
        [frontends."frontend-{{ $frontendName }}".passTLSClientCert.infos.issuer]
          country = {{ $issuer.Country }}
          province = {{ $issuer.Province }}
          locality = {{ $issuer.Locality }}
          organization = {{ $issuer.Organization }}
          commonName = {{ $issuer.CommonName }}
          serialNumber = {{ $issuer.SerialNumber }}
          domainComponent = {{ $issuer.DomainComponent }}
        {{end}}
      {{end}}
    {{end}}

    {{ $auth := getAuth $container.SegmentLabels }}
    {{if $auth }}
    [frontends."frontend-{{ $frontendName }}".auth]
      headerField = "{{ $auth.HeaderField }}"

      {{if $auth.Forward }}
      [frontends."frontend-{{ $frontendName }}".auth.forward]
        address = "{{ $auth.Forward.Address }}"
        trustForwardHeader = {{ $auth.Forward.TrustForwardHeader }}
        {{if $auth.Forward.AuthResponseHeaders }}
        authResponseHeaders = [{{range $auth.Forward.AuthResponseHeaders }}
          "{{.}}",
          {{end}}]
        {{end}}

        {{if $auth.Forward.TLS }}
        [frontends."frontend-{{ $frontendName }}".auth.forward.tls]
          ca = "{{ $auth.Forward.TLS.CA }}"
          caOptional = {{ $auth.Forward.TLS.CAOptional }}
          cert = """{{ $auth.Forward.TLS.Cert }}"""
          key = """{{ $auth.Forward.TLS.Key }}"""
          insecureSkipVerify = {{ $auth.Forward.TLS.InsecureSkipVerify }}
        {{end}}
      {{end}}

      {{if $auth.Basic }}
      [frontends."frontend-{{ $frontendName }}".auth.basic]
        realm = "{{ $auth.Basic.Realm }}"
        removeHeader = {{ $auth.Basic.RemoveHeader }}
        {{if $auth.Basic.Users }}
        users = [{{range $auth.Basic.Users }}
          "{{.}}",
          {{end}}]
        {{end}}
        usersFile = "{{ $auth.Basic.UsersFile }}"
      {{end}}

      {{if $auth.Digest }}
      [frontends."frontend-{{ $frontendName }}".auth.digest]
        removeHeader = {{ $auth.Digest.RemoveHeader }}
        {{if $auth.Digest.Users }}
        users = [{{range $auth.Digest.Users }}
          "{{.}}",
          {{end}}]
        {{end}}
        usersFile = "{{ $auth.Digest.UsersFile }}"
      {{end}}
    {{end}}

    {{ $whitelist := getWhiteList $container.SegmentLabels }}
    {{if $whitelist }}
    [frontends."frontend-{{ $frontendName }}".whiteList]
      sourceRange = [{{range $whitelist.SourceRange }}
        "{{.}}",
        {{end}}]
      {{if $whitelist.IPStrategy }}
      [frontends."frontend-{{ $frontendName }}".whiteList.IPStrategy]
        depth = {{ $whitelist.IPStrategy.Depth }}
        excludedIPs = [{{range $whitelist.IPStrategy.ExcludedIPs }}
          "{{.}}",
          {{end}}]
      {{end}}
    {{end}}

    {{ $redirect := getRedirect $container.SegmentLabels }}
    {{if $redirect }}
    [frontends."frontend-{{ $frontendName }}".redirect]
      entryPoint = "{{ $redirect.EntryPoint }}"
      regex = "{{ $redirect.Regex }}"
      replacement = "{{ $redirect.Replacement }}"
      permanent = {{ $redirect.Permanent }}
    {{end}}

    {{ $errorPages := getErrorPages $container.SegmentLabels }}
    {{if $errorPages }}
    [frontends."frontend-{{ $frontendName }}".errors]
      {{range $pageName, $page := $errorPages }}
      [frontends."frontend-{{ $frontendName }}".errors."{{ $pageName }}"]
        status = [{{range $page.Status }}
          "{{.}}",
          {{end}}]
        backend = "backend-{{ $page.Backend }}"
        query = "{{ $page.Query }}"
      {{end}}
    {{end}}

    {{ $rateLimit := getRateLimit $container.SegmentLabels }}
    {{if $rateLimit }}
    [frontends."frontend-{{ $frontendName }}".rateLimit]
      extractorFunc = "{{ $rateLimit.ExtractorFunc }}"
      [frontends."frontend-{{ $frontendName }}".rateLimit.rateSet]
        {{ range $limitName, $limit := $rateLimit.RateSet }}
        [frontends."frontend-{{ $frontendName }}".rateLimit.rateSet."{{ $limitName }}"]
          period = "{{ $limit.Period }}"
          average = {{ $limit.Average }}
          burst = {{ $limit.Burst }}
        {{end}}
    {{end}}

    {{ $headers := getHeaders $container.SegmentLabels }}
    {{if $headers }}
    [frontends."frontend-{{ $frontendName }}".headers]
      SSLRedirect = {{ $headers.SSLRedirect }}
      SSLTemporaryRedirect = {{ $headers.SSLTemporaryRedirect }}
      SSLHost = "{{ $headers.SSLHost }}"
      SSLForceHost = {{ $headers.SSLForceHost }}
      STSSeconds = {{ $headers.STSSeconds }}
      STSIncludeSubdomains = {{ $headers.STSIncludeSubdomains }}
      STSPreload = {{ $headers.STSPreload }}
      ForceSTSHeader = {{ $headers.ForceSTSHeader }}
      FrameDeny = {{ $headers.FrameDeny }}
      CustomFrameOptionsValue = "{{ $headers.CustomFrameOptionsValue }}"
      ContentTypeNosniff = {{ $headers.ContentTypeNosniff }}
      BrowserXSSFilter = {{ $headers.BrowserXSSFilter }}
      ContentSecurityPolicy = "{{ $headers.ContentSecurityPolicy }}"
      CustomBrowserXSSValue = "{{ $headers.CustomBrowserXSSValue }}"
      PublicKey = "{{ $headers.PublicKey }}"
      ReferrerPolicy = "{{ $headers.ReferrerPolicy }}"
      IsDevelopment = {{ $headers.IsDevelopment }}

      {{if $headers.AllowedHosts }}
      AllowedHosts = [{{range $headers.AllowedHosts }}
        "{{.}}",
        {{end}}]
      {{end}}

      {{if $headers.HostsProxyHeaders }}
      HostsProxyHeaders = [{{range $headers.HostsProxyHeaders }}
        "{{.}}",
        {{end}}]
      {{end}}

      {{if $headers.CustomRequestHeaders }}
      [frontends."frontend-{{ $frontendName }}".headers.customRequestHeaders]
        {{range $k, $v := $headers.CustomRequestHeaders }}
        {{$k}} = "{{$v}}"
        {{end}}
      {{end}}

      {{if $headers.CustomResponseHeaders }}
      [frontends."frontend-{{ $frontendName }}".headers.customResponseHeaders]
        {{range $k, $v := $headers.CustomResponseHeaders }}
        {{$k}} = "{{$v}}"
        {{end}}
      {{end}}

      {{if $headers.SSLProxyHeaders }}
      [frontends."frontend-{{ $frontendName }}".headers.SSLProxyHeaders]
        {{range $k, $v := $headers.SSLProxyHeaders }}
        {{$k}} = "{{$v}}"
        {{end}}
      {{end}}

    {{end}}

    [frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"]
      rule = "{{ getFrontendRule $container $container.SegmentLabels }}"

{{end}}