<commit_before>fs = require 'fs'

createUpstreams = (workers={}) ->
  upstreams = "# add global upstreams\n"
  for name, options of workers when options.ports?.incoming?
    servers = ""
    { incoming: port } = options.ports
    options.instances or= 1
    for index in [0...options.instances]
      servers += "\n" if servers isnt ""
      port = parseInt(port, 10)

      servers += "\tserver 127.0.0.1:#{port + index};"

    upstreams += """
      upstream #{name} {
    #{servers}
      }\n\n"""

  return upstreams


basicAuth = """
auth_basic            "Restricted";
      auth_basic_user_file  /etc/nginx/conf.d/.htpasswd;"""

allowInternal = """
  allow                 127.0.0.0/8;
        allow                 10.0.0.0/16;
        allow                 192.168.0.0/16;
        allow                 172.16.0.0/12;
        deny                  all;
"""

createWebLocation = ({name, locationConf}) ->
  { location, proxyPass, internalOnly, auth } = locationConf
  return """\n
      location #{location} {
        proxy_pass            #{proxyPass};
        proxy_set_header      X-Real-IP       $remote_addr;
        proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_next_upstream   error timeout   invalid_header http_500;
        proxy_connect_timeout 1;
        #{if internalOnly then allowInternal else ''}
        #{if auth then basicAuth else ''}
      }
  \n"""

createWebsocketLocation = ({name, locationConf, proxyPass}) ->
  {location, proxyPass} = locationConf
  return """\n
      location #{location} {
        proxy_pass            #{proxyPass};

        # needed for websocket handshake
        proxy_http_version    1.1;
        proxy_set_header      Upgrade         $http_upgrade;<commit_msg>Revert "nginx: Add koding-prod-deployment CIDR to allowed internal subnets"<commit_after>fs = require 'fs'

createUpstreams = (workers={}) ->
  upstreams = "# add global upstreams\n"
  for name, options of workers when options.ports?.incoming?
    servers = ""
    { incoming: port } = options.ports
    options.instances or= 1
    for index in [0...options.instances]
      servers += "\n" if servers isnt ""
      port = parseInt(port, 10)

      servers += "\tserver 127.0.0.1:#{port + index};"

    upstreams += """
      upstream #{name} {
    #{servers}
      }\n\n"""

  return upstreams


basicAuth = """
auth_basic            "Restricted";
      auth_basic_user_file  /etc/nginx/conf.d/.htpasswd;"""

allowInternal = """
  allow                 127.0.0.0/8;
        allow                 192.168.0.0/16;
        allow                 172.16.0.0/12;
        deny                  all;
"""

createWebLocation = ({name, locationConf}) ->
  { location, proxyPass, internalOnly, auth } = locationConf
  return """\n
      location #{location} {
        proxy_pass            #{proxyPass};
        proxy_set_header      X-Real-IP       $remote_addr;
        proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_next_upstream   error timeout   invalid_header http_500;
        proxy_connect_timeout 1;
        #{if internalOnly then allowInternal else ''}
        #{if auth then basicAuth else ''}
      }
  \n"""

createWebsocketLocation = ({name, locationConf, proxyPass}) ->
  {location, proxyPass} = locationConf
  return """\n
      location #{location} {
        proxy_pass            #{proxyPass};

        # needed for websocket handshake
        proxy_http_version    1.1;
        proxy_set_header      Upgrade         $http_upgrade;
{'fraction_non_alphanumeric': 0.11864887879648027, 'fraction_numerical': 0.025830258302583026, 'mean_word_length': 2.0670147954743254, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 9, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3183398', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1082, 'n_words': 249}