Websites/lib/middleware/custom_static_middleware.rb
Donovan Daniels 90c05547f6
Move dev host rewriting into middleware
This makes matching against routes later much more reliable and less complicated
2024-05-03 17:53:47 -05:00

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