require 'rubygems'
require 'eventmachine'
require 'evma_httpserver'

class Handler  < EventMachine::Connection
  include EventMachine::HttpServer

  def process_http_request
    resp = EventMachine::DelegatedHttpResponse.new( self )
                resp.keep_connection_open(false)

    http = EM::Protocols::HttpClient.request(
              :host=>"67.202.54.149",
              :port=>8080,
              :request=>"/test.js"
           )

    # once download is complete, send it to client
    http.callback do |r|
      resp.status = 200
      resp.content = r[:content]
      resp.send_response
    end

    http.errback do |r|
       p 'fail'
       resp.status = 500
       resp.send_response
    end

  end
end

EventMachine.epoll
EventMachine::run {
  EventMachine::start_server("0.0.0.0", 8082, Handler)
  puts "Listening..."
}