Skip to content

Instantly share code, notes, and snippets.

@toolmantim
Last active November 18, 2020 17:23
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save toolmantim/6632690 to your computer and use it in GitHub Desktop.
Save toolmantim/6632690 to your computer and use it in GitHub Desktop.
Reconfigures Rails ActionDispatch's TLD handling dynamically based on the request host, so you don't have to mess with config.action_dispatch.tld_length for cross-device testing using xip.io and friends
# Reconfigures ActionDispatch's TLD handling dynamically based on the request
# host, so you don't have to mess with config.action_dispatch.tld_length for
# cross-device testing using xip.io and friends
#
# Examples:
# use Rack::HostBasedTldLength, /xip\.io/, 5
class Rack::HostBasedTldLength
def initialize(app, host_pattern, host_tld_length)
@app = app
@host_pattern = Regexp.new(host_pattern)
@host_tld_length = host_tld_length
end
def call(env)
original_tld_length = tld_length
request = Rack::Request.new(env)
set_tld_length(@host_tld_length) if request.host =~ @host_pattern
@app.call(env)
ensure
set_tld_length(original_tld_length)
end
private
def tld_length
ActionDispatch::Http::URL.tld_length
end
def set_tld_length(length)
ActionDispatch::Http::URL.tld_length = length
end
end
@jmuheim
Copy link

jmuheim commented Feb 12, 2014

For the unknowing ones (like me):

  1. Put this code into lib/rack/host_based_tld_length.rb
  2. Add config.middleware.use Rack::HostBasedTldLength, /xip\.io/, 5 to your config/development.rb
  3. Connect to your app using 192.168.1.123.xip.io:3000 (when 192.168.1.123 is your local IP); you can even connect to a subdomain of your app like this: subdomain.192.168.1.123.xip.io:3000!

Thanks for this nice piece of code! 👍

@coneybeare
Copy link

awesome, thanks

@johnhamelink
Copy link

I've taken the liberty of pulling this into a Gem to for everyone to use: https://github.com/WeAreFarmGeek/tld_length

@catkins
Copy link

catkins commented Oct 30, 2014

@toolmantim @johnhamelink you guys are the best, this is so useful.

@fphilipe
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment