Getting the ngrok URL

less than 1 minute read

A quick addendum to my previous ngrok post. If you are not using subdomains, it can be useful for your app to know what ngrok’s dynamically generated URL is. The simplest way to do that is to set an environmental variable. I like APP_URL, but you can use whatever.

Just add:

ENV['APP_URL'] = Ngrok::Tunnel.ngrok_url_https

to config/puma.rb after the ngrok tunnel is started. The entire code becomes:

if Rails.env.development?
  require 'ngrok/tunnel'
  options = {   addr: ENV.fetch("PORT") { 3000 },
                config: File.join(ENV['HOME'],'.ngrok2','ngrok.yml')
  options[:subdomain] = ENV['NGROK_SUBDOMAIN'] if ENV['NGROK_SUBDOMAIN']
  puts "[NGROK] tunneling at " + Ngrok::Tunnel.start(options)
  ENV['APP_URL'] = Ngrok::Tunnel.ngrok_url_https

(I’m using the HTTPS URL, but you can swap it out for Ngrok::Tunnel.ngrok_url.)

With that, you can do things like automatically set callback URLs:

<a href="<%=
ENV['APP_URL'] %>/oauth">Login</a>

There is an alternative to approach, using request.base_url as in:

<a href="<%= request.base_url %>/oauth">Login</a>

This works fine if you are handling a web request in a controller/view, but isn’t available in models/libraries (unless you pass it in).

Finally, the above is needed to handle a hostname that isn’t static. If it is, you can just set:

Rails.application.routes.default_url_options[:host] ='

in config/environments/{development,production}.rb.

Tags: ,