Websites/lib/custom_static_middleware.rb
2024-05-02 22:04:43 -05:00

29 lines
557 B
Ruby

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