Class: StaticRails::ProxyMiddleware

Inherits:
Rack::Proxy
  • Object
show all
Defined in:
lib/static-rails/proxy_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ProxyMiddleware

Returns a new instance of ProxyMiddleware.



8
9
10
11
12
13
# File 'lib/static-rails/proxy_middleware.rb', line 8

def initialize(app)
  @matches_request_to_static_site = MatchesRequestToStaticSite.new
  @app = app
  @servers = {}
  super
end

Instance Method Details

#perform_request(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/static-rails/proxy_middleware.rb', line 15

def perform_request(env)
  return @app.call(env) unless StaticRails.config.proxy_requests

  server_store = ServerStore.instance
  server_store.ensure_all_servers_are_started

  req = Rack::Request.new(env)
  if (req.get? || req.head?) && (site = @matches_request_to_static_site.call(req))
    if site.ping_server && (server = server_store.server_for(site))
      server.wait_until_ready
    end

    @backend = URI("http://#{site.server_host}:#{site.server_port}")
    env["HTTP_HOST"] = @backend.host
    env["PATH_INFO"] = forwarding_path(site, req)

    super(env)
  else
    @app.call(env)
  end
end