class StaticRails::ProxyMiddleware

Public Class Methods

new(app) click to toggle source
Calls superclass method
# File lib/static-rails/proxy_middleware.rb, line 8
def initialize(app)
  @matches_request_to_static_site = MatchesRequestToStaticSite.new
  @app = app
  @servers = {}
  super
end

Public Instance Methods

perform_request(env) click to toggle source
Calls superclass method
# 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

Private Instance Methods

forwarding_path(site, req) click to toggle source
# File lib/static-rails/proxy_middleware.rb, line 39
def forwarding_path(site, req)
  req_path = req.path_info

  if req_path == site.url_root_path && !req_path.end_with?("/")
    req_path + "/" # <- Necessary for getting jekyll, possibly hugo to serve the root
  else
    req_path
  end
end