Class: Rack::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/rambo/middleware/proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Proxy

Returns a new instance of Proxy.



6
7
8
9
10
# File 'lib/rambo/middleware/proxy.rb', line 6

def initialize(app, options={})
  @app = app
  @options = options
  @@conn = Rightscale::HttpConnection.new
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rambo/middleware/proxy.rb', line 12

def call(env)
  
  request = Request.new(env)
  
  # tey out persistent connections using right http conn (need to make params aware?)
  
  begin
    
    backend = rand(@options[:backend].size+1)
    
    if backend == @options[:backend].size
      @app.call(env)
    else
      host = URI.parse("http://#{env['HTTP_HOST']}").host
      port = @options[:backend][backend]
  
      req = Net::HTTP::Get.new(env['REQUEST_URI'])
      res = Net::HTTP.start(host, port) {|http|
         http.request(req, request.params)
       }

      # need to be able to pass params into request below
      #res = @@conn.req(:server => host, :port => port, :protocol => 'http', :request => request)
    
      body = res.body || ''
    
      [res.code.to_i, {'ETag' => res['ETag'], 'Cache-Control' => res['Cache-Control'], 'Content-Type' => res['Content-Type'], 'Location' => res['Location']}, body]
    end
  rescue Exception => e
    puts e.message
    @app.call(env)
  end
end