-
Notifications
You must be signed in to change notification settings - Fork 7
/
ngrok.rb
37 lines (30 loc) · 959 Bytes
/
ngrok.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'socket'
def wait
# Block execution until the ngrok is ready
started = false
until started do
sleep 0.5
begin
TCPSocket.new('localhost', 4040)
started = true
puts "ngrok started"
rescue Errno::ECONNREFUSED => e
puts "Waiting for ngrok to start"
end
end
sleep 0.5
end
def tunnel(config)
%Q{curl -X POST -H "Accept: application/json" -H "Content-type: application/json" -d '#{config}' http://localhost:4040/api/tunnels}
end
def config(subdomain, port)
%Q{{ "addr": "#{port}", "proto": "http", "subdomain": "#{subdomain}", "name": "#{subdomain}" }}
end
wait
puts "Starting tunnels"
app = tunnel(config(ENV['APP_SUBDOMAIN'], ENV['APP_PORT']))
assets = tunnel(config(ENV['ASSETS_SUBDOMAIN'], ENV['ASSETS_PORT']))
system app
sleep 0.5 # Sleep is ugly, but if you make request to ngrok too quickly it will panic and die.
system assets
sleep # Don't exit. Sleep forever so that Foreman doesn't die