Last active
December 15, 2015 01:19
-
-
Save rafaelsales/5179359 to your computer and use it in GitHub Desktop.
Create local proxy server associated with public url whenever starting rails server in development mode. It uses proxylocal.com service and gem to generate the public url.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This initializer creates a local proxy server that makes the local web server accessible | |
# via a public host generated by ProxyLocal service - http://www.proxylocal.com | |
Rails.application.config.after_initialize do | |
server_running = defined?(WEBrick) || defined?(Rails::Server) || defined?(PhusionPassenger) ? true : false | |
next unless Rails.env.development? && server_running | |
Thread.new do | |
ProxyLocal::Client.run({ | |
server_host: 'proxylocal.com', | |
server_port: '8282', | |
local_port: '3000', | |
tls: false, | |
verbose: false | |
}) | |
end | |
end | |
def define_default_rails_public_url(url) | |
uri = URI(url) | |
Rails.application.routes.default_url_options.merge!(host: uri.host, port: uri.port, protocol: uri.scheme) | |
end | |
# Opening ProxyLocal class to capture the public URL | |
class ProxyLocal::Client | |
def receive_message_with_capture(message) | |
url = message[/http.*com/] | |
define_default_rails_public_url(url) if url | |
receive_message_without_capture(message) | |
end | |
alias_method_chain :receive_message, :capture | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment