<commit_before>httpProxy = require 'http-proxy'

# The proxy is one of the main building blocks of this server. It knows where
# (on which port) an app is deployed and will proxy to the application in
# different ways.
#
# == IMPLEMENTED ==
# 1. Proxy using the name. I the "X-App" header contains a known application
#    name the server will proxy to the corresponding application.
#
# == NOT IMPLEMENTED ==
# 2. Proxy using a dns / domain regex. If the header matches the domain config
#    of the application. ()
# 3. The application port is used.
# 4. Using a path in the request.
#
class Proxy
  constructor: (startPort) ->
    @localhost = '127.0.0.1'
    @global = '0.0.0.0'
    @startPort = startPort || 5000
    @maxPorts = 1000
    @endPort = @startPort + @maxPorts
    @ports = {}
    @apps = {}
    @appHeader = "x-app"
    @loadBalancer = {}
    
    @server = httpProxy.createServer (req, res, proxy) =>
      # Proxy using app name (Header)
      if appName = req.headers[@appHeader]
        if ports = @apps[appName]
          @loadBalancer[appName] += 1
          port = ports[@loadBalancer[appName] % ports.length]

          console.log "proxy to #{appName}:#{port}"
          # redirect to the application
          proxy.proxyRequest req, res,
            host: @localhost
            port: port
        else
          # App not found
          @clientError res, "Error: app '#{appName}' don't exist!"
      else
        # No app passed
        @clientError res, "Error: Need to pass the 'X-App' Header"

  # Register the application and start it so that the proxy can start proxying
  # requests to it.
  # @param [String] name
  # @param [Process] app the app process
  registerApp: (name, app) ->
    port = @registerPort(name, app)
    app.listen(port, @localhost)
    console.log "Started app '#{app.name()}' on #{@localhost}:#{port}"

  # Register the application and find a free port for it.
  # @param [String] name
  # @param [Process] app the app process
  registerPort: (name, app) ->
    for port in [@startPort..@endPort]
      unless @ports[port]
        # Register the port for the application
        @ports[port] = app
        
        # Create application name base lookups for ports
        @apps[name] ||= []
        @apps[name].push port
        @loadBalancer[name] ||= 0
<commit_msg>moved ports and port load balancing to its own method<commit_after>httpProxy = require 'http-proxy'

# The proxy is one of the main building blocks of this server. It knows where
# (on which port) an app is deployed and will proxy to the application in
# different ways.
#
# == IMPLEMENTED ==
# 1. Proxy using the name. I the "X-App" header contains a known application
#    name the server will proxy to the corresponding application.
#
# == NOT IMPLEMENTED ==
# 2. Proxy using a dns / domain regex. If the header matches the domain config
#    of the application. ()
# 3. The application port is used.
# 4. Using a path in the request.
#
class Proxy
  constructor: (startPort) ->
    @localhost = '127.0.0.1'
    @global = '0.0.0.0'
    @startPort = startPort || 5000
    @maxPorts = 1000
    @endPort = @startPort + @maxPorts
    @ports = {}
    @apps = {}
    @appHeader = "x-app"
    @loadBalancer = {}
    
    @server = httpProxy.createServer (req, res, proxy) =>
      # Proxy using app name (Header)
      if appName = req.headers[@appHeader]
        if port = @portForApp(appName)
          # redirect to the application
          proxy.proxyRequest req, res,
            host: @localhost
            port: port
        else
          # App not found
          @clientError res, "Error: app '#{appName}' don't exist!"
      else
        # No app passed
        @clientError res, "Error: Need to pass the 'X-App' Header"

  portForApp: (appName) ->
    if ports = @apps[appName]
      @loadBalancer[appName] += 1
      port = ports[@loadBalancer[appName] % ports.length]
      port

  # Register the application and start it so that the proxy can start proxying
  # requests to it.
  # @param [String] name
  # @param [Process] app the app process
  registerApp: (name, app) ->
    port = @registerPort(name, app)
    app.listen(port, @localhost)
    console.log "Started app '#{app.name()}' on #{@localhost}:#{port}"

  # Register the application and find a free port for it.
  # @param [String] name
  # @param [Process] app the app process
  registerPort: (name, app) ->
    for port in [@startPort..@endPort]
      unless @ports[port]
        # Register the port for the application
        @ports[port] = app
        
        # Create application name base lookups for ports
        @apps[name] ||= []
        @apps[name].push port
        @loadBalancer[name] ||= 0

{'fraction_non_alphanumeric': 0.11196090928404503, 'fraction_numerical': 0.010197578075207138, 'mean_word_length': 2.5612708018154313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7173561', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1407, 'n_words': 525}