Donovan Daniels
90c05547f6
This makes matching against routes later much more reliable and less complicated
31 lines
588 B
Ruby
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
|