# All files / src/middleware proxy.coffee

64.71% Statements 11/17
100% Branches 0/0
0% Functions 0/3
64.71% Lines 11/17

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    291x
    1x

    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x

    1x

     logger = require "winston"
    Q = require "q"

    config = require '../config/config'
    statsdServer = config.get 'statsd'
    application = config.get 'application'
    SDC = require 'statsd-client'
    os = require 'os'

    domain = "#{os.hostname()}.#{application.name}.appMetrics"
    sdc = new SDC statsdServer

    exports.setupProxyHeaders = setupProxyHeaders = (ctx) ->
      # Headers
      setOrAppendHeader = (ctx, header, value) ->
        return if not value
        if ctx.header[header]
          ctx.header[header] = "#{ctx.header[header]}, #{value}"
        else
          ctx.header[header] = "#{value}"
      setOrAppendHeader ctx, 'X-Forwarded-For', ctx.request.ip
      setOrAppendHeader ctx, 'X-Forwarded-Host', ctx.request.host

    exports.koaMiddleware = (next) ->
      startTime = new Date() if statsdServer.enabled
      exports.setupProxyHeaders this
      sdc.timing "#{domain}.proxyHeadersMiddleware", startTime if statsdServer.enabled
      yield next