###################################
# Generated by confd {{datetime}} #
#             #######             #
#             # k3d #             #
#             #######             #
###################################

error_log stderr notice;

worker_processes auto;
events {
  multi_accept on;
  use epoll;
  worker_connections {{ getv "/settings/workerConnections" }};
}

stream {

  {{- range $portstring := lsdir "/ports" }}


  {{- $portdir := printf "/ports/%s/*" $portstring -}}
  {{- $port := index (split $portstring ".") 0 -}}
  {{- $protocol := index (split $portstring ".") 1 -}}
  {{- $upstream := replace $portstring "." "_" -1 }}

  upstream {{ $upstream }} {
    {{- range $server := getvs $portdir }}
    server {{ $server }}:{{ $port }} max_fails=1 fail_timeout=10s;
    {{- end }}
  }

  server {
    listen        {{ $port }} {{- if (eq $protocol "udp") }} udp{{- end -}};
    proxy_pass    {{ $upstream }};
    proxy_timeout {{ getv "/settings/defaultProxyTimeout" "600" }};
    proxy_connect_timeout 2s;
  }

  
  {{- end }}

}
