diff --git a/config/application.rb b/config/application.rb index 8af79dd..3b60fd7 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true require_relative "boot" -require_relative "../lib/middleware/custom_static_middleware" -require_relative "../lib/middleware/dev_host_middleware" +require_relative "../lib/middleware/custom_static" +require_relative "../lib/middleware/dev_host" require "rails/all" @@ -36,8 +36,8 @@ module Websites config.action_controller.action_on_unpermitted_parameters = :raise config.action_dispatch.default_headers.clear - config.middleware.insert_before(0, DevHostMiddleware) if Rails.env.development? - config.middleware.insert_before(ActionDispatch::Static, CustomStaticMiddleware, { + config.middleware.insert_before(0, Middleware::DevHost) if Rails.env.development? + config.middleware.insert_before(ActionDispatch::Static, Middleware::CustomStatic, { /^i\.furry\.cool/ => "/furry.cool/images", /^i\.maidboye\.cafe/ => "/maidboye.cafe/images", %r{^maidboye\.cafe/images} => "/maidboye.cafe", diff --git a/lib/middleware/custom_static.rb b/lib/middleware/custom_static.rb new file mode 100644 index 0000000..97a1f8e --- /dev/null +++ b/lib/middleware/custom_static.rb @@ -0,0 +1,32 @@ +# 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 diff --git a/lib/middleware/custom_static_middleware.rb b/lib/middleware/custom_static_middleware.rb deleted file mode 100644 index 5c31db6..0000000 --- a/lib/middleware/custom_static_middleware.rb +++ /dev/null @@ -1,30 +0,0 @@ -# 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 diff --git a/lib/middleware/dev_host.rb b/lib/middleware/dev_host.rb new file mode 100644 index 0000000..34c8e62 --- /dev/null +++ b/lib/middleware/dev_host.rb @@ -0,0 +1,16 @@ +module Middleware + class DevHost + def initialize(app) + @app = app + end + + def call(env) + request = Rack::Request.new(env) + domain = request.params["domain"] + + env["HTTP_HOST"] = domain if Rails.env.development? && domain + + @app.call(env) + end + end +end diff --git a/lib/middleware/dev_host_middleware.rb b/lib/middleware/dev_host_middleware.rb deleted file mode 100644 index f9b12e9..0000000 --- a/lib/middleware/dev_host_middleware.rb +++ /dev/null @@ -1,14 +0,0 @@ -class DevHostMiddleware - def initialize(app) - @app = app - end - - def call(env) - request = Rack::Request.new(env) - domain = request.params["domain"] - - env["HTTP_HOST"] = domain if Rails.env.development? && domain - - @app.call(env) - end -end