{{- $servers := split (getenv "SERVERS") "," -}}
{{- $ports := split (getenv "PORTS") "," -}}
{{- $udp_ports := split (getenv "UDP_PORTS") "," -}}
error_log stderr notice;

worker_processes auto;
events {
  multi_accept on;
  use epoll;
  worker_connections {{ add 1024 (len $ports) }};
}

stream {

  #######
  # TCP #
  #######
  {{- range $port := $ports }}
  upstream server_nodes_{{ $port }} {
    {{- range $server := $servers }}
    server {{ $server }}:{{ $port }} max_fails=1 fail_timeout=10s;
    {{- end }}
  }

  server {
    listen        {{ $port }};
    proxy_pass    server_nodes_{{ $port }};
    proxy_timeout 600;
    proxy_connect_timeout 2s;
  }
  {{- end }}

  #######
  # UDP #
  #######

  {{- range $port := $udp_ports }}
  {{- if $port }}
  upstream server_nodes_udp_{{ $port }} {
    {{- range $server := $servers }}
    server {{ $server }}:{{ $port }} max_fails=1 fail_timeout=10s;
    {{- end }}
  }

  server {
    listen        {{ $port }} udp;
    proxy_pass    server_nodes_udp_{{ $port }};
    proxy_timeout 600;
    proxy_connect_timeout 2s;
  }
  {{- end }}
  {{- end }}
}
