Websites/lib/custom_static_middleware.rb

31 lines
588 B
Ruby

# frozen_string_literal: true
class CustomStaticMiddleware
def initialize(app, domain_map)
@app = app
@domain_map = domain_map
end
def call(env)
request = Rack::Request.new(env)
host = request.host
if (target_path = find_mapped_path(host, request.path))
env["PATH_INFO"] = target_path
end
@app.call(env)
end
private
def find_mapped_path(host, path)
@domain_map.each do |domain_pattern, subdirectory|
matcher = "#{host}#{path}"
return "#{subdirectory}#{path}" if matcher.match?(domain_pattern)
end
nil
end
end