Skip to content

Instantly share code, notes, and snippets.

@palkan
Created November 7, 2017 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palkan/c5b19d1a09ac63a0d9b08111877b1f99 to your computer and use it in GitHub Desktop.
Save palkan/c5b19d1a09ac63a0d9b08111877b1f99 to your computer and use it in GitHub Desktop.
RackRewriteConfig
class RackRewriteConfig
class << self
# Configure named rewrite rule
def configure(name, &block)
raise ArgumentError, "Block is required" unless block_given?
rules[name] = block
end
# Apply named rule to the target
def apply(target, name, *args)
raise "Rule not found #{name}" unless rules.key?(name)
target.instance_exec(*args, &rules[name])
end
private
def rules
@rules ||= {}
end
end
end
Rails.application.configure do
config.middleware.insert_before(Rack::Runtime, Rack::Rewrite) do
LegacyRoutes.apply(self, :domain_redirect, host: 'bulls.com', target: 'https://www.bulls.com')
LegacyRoutes.apply(self, :legacy_callbacks, host: 'cows.com', target: 'callbacks.bulls.com')
LegacyRoutes.apply(self, :legacy_callbacks, host: 'www.cows.com', target: 'callbacks.bulls.com')
LegacyRoutes.apply(self, :domain_redirect, host: 'cows.com', target: 'https://www.bulls.com')
LegacyRoutes.apply(self, :domain_redirect, host: 'www.cows.com', target: 'https://www.bulls.com')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment