Websites/lib/middleware/custom_static.rb

33 lines
644 B
Ruby
Raw Permalink Normal View History

2024-05-03 23:06:08 +00:00
# frozen_string_literal: true
module Middleware
class CustomStatic
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
end