Move dev host rewriting into middleware

This makes matching against routes later much more reliable and less complicated
This commit is contained in:
Donovan Daniels 2024-05-03 17:51:05 -05:00
parent b9a09b228b
commit 90c05547f6
Signed by: Donovan_DMC
GPG Key ID: 907D29CBFD6157BA
5 changed files with 30 additions and 34 deletions

View File

@ -1,7 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
require_relative "boot" require_relative "boot"
require_relative "../lib/custom_static_middleware" require_relative "../lib/middleware/custom_static_middleware"
require_relative "../lib/middleware/dev_host_middleware"
require "rails/all" require "rails/all"
@ -35,6 +36,7 @@ module Websites
config.action_controller.action_on_unpermitted_parameters = :raise config.action_controller.action_on_unpermitted_parameters = :raise
config.action_dispatch.default_headers.clear 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(ActionDispatch::Static, CustomStaticMiddleware, {
/^i\.furry\.cool/ => "/furry.cool/images", /^i\.furry\.cool/ => "/furry.cool/images",
/^i\.maidboye\.cafe/ => "/maidboye.cafe/images", /^i\.maidboye\.cafe/ => "/maidboye.cafe/images",

View File

@ -135,6 +135,8 @@ module Websites
end end
def common_headers(domain) def common_headers(domain)
# We don't want to set headers like HSTS in development
return {} if Rails.env.development?
{ {
"Report-To": { "Report-To": {
group: "default", group: "default",

View File

@ -3,47 +3,25 @@
class DomainConstraint class DomainConstraint
attr_reader :domain, :subdomain attr_reader :domain, :subdomain
DEFAULT_DOMAIN = nil # "yiff.rest".freeze DEFAULT_DOMAIN = nil # "yiff.rest"
USE_DEV_HOST = Rails.env.development?
def dev_host
"#{domain.tr('.', '-')}.websites.containers.local"
end
def initialize(domain, subdomain = nil) def initialize(domain, subdomain = nil)
@domain = domain @domain = domain
@subdomain = subdomain @subdomain = subdomain
end end
def resolve_domains(current_host, level = 1)
current_subdomains = nil
if current_host.scan(".").length > level
parts = current_host.split(".")
current_host = parts.pop(level + 1).join(".")
current_subdomains = parts.join(".") if parts.length >= 1
end
[current_host, current_subdomains]
end
def matches?(request) def matches?(request)
host = domain Rails.logger.info("Domain: #{@domain}; Subdomain: #{@subdomain}; Current Host: #{request.domain}; Current Subdomains: #{request.subdomain}; Matches (Domain): #{domain_matches?(request)}; Matches (Subdomain): #{subdomain_matches?(request)}") if Rails.env.development?
parse = request.domain subdomain_matches?(request) && domain_matches?(request)
parse = "#{request.subdomains.join('.')}.#{request.domain}" unless request.subdomains.empty?
current_host, current_subdomains = resolve_domains(parse)
if Rails.env.development? && USE_DEV_HOST
if DEFAULT_DOMAIN.present?
current_host, current_subdomains = resolve_domains(DEFAULT_DOMAIN)
elsif request.query_parameters[:domain].present?
current_host, current_subdomains = resolve_domains(request.query_parameters[:domain])
elsif current_host.scan(".").length > 3
current_host, current_subdomains = resolve_domains(current_host, 3)
end end
current_host = dev_host if current_host == domain private
host = dev_host
def domain_matches?(request)
@domain.nil? || request.domain == @domain
end end
Rails.logger.info("Host: #{host}; Subdomain: #{subdomain}; Current Host: #{current_host}; Current Subdomain: #{current_subdomains}; Matches (Domain): #{current_host == host}; Matches (Subdomain): #{current_subdomains == subdomain || subdomain&.to_sym == :any}") if Rails.env.development?
current_host == host && (current_subdomains == subdomain || subdomain&.to_sym == :any) def subdomain_matches?(request)
@subdomain.nil? || request.subdomain == @subdomain
end end
end end

View File

@ -0,0 +1,14 @@
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