Ask Ubuntu Asked by tidelake on January 15, 2021
I have a webapp I’d like to start with a script, rather than open 3 tabs and start the processes, then change a line to a config file, and then start the web server.
First, so I can test webhooks, I need to run ngrok http https://localhost:3000
, which yields something just as so–
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online
Account [email protected] (Plan: Free)
Version 2.3.35
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://2f22e8990cbe.ngrok.io -> https://localhost:3000
Forwarding https://2f22e8990cbe.ngrok.io -> https://localhost:3000
Connections ttl opn rt1 rt5 p50 p90
13973 1 0.00 0.07 0.00 0.00
I then change a config file to accommodate the new host (random for the free usage of ngrok
)
# development.rb
config.hosts << '2f22e8990cbe.ngrok.io' # (tunnel address from above)
I then save the file and start the webserver.
$ rails server puma --bind 'ssl://127.0.0.1:3000?key=/home/oaty/.ssh/localhost.key&cert=/home/oaty/.ssh/localhost.crt'
I’d like to script this into one command. I can use a larger, shell environment variable for the ngrok host, tho I have to get that somehow from the same script, to begin with. I hope I’m not asking too much. I really love the Ubuntu forums. I’m too Autistic, I guess, for most other places. Viva Ubuntu!
I found a solution by drilling down more accurately. I've adapted the code a little. The curl
with jq
command is the real pearl. Thank you.
(Note: localhost:4040 is ngrok
's web ui page.)
#!/bin/bash
sh -c 'ngrok http https://localhost:3000 &'
sleep 2
curl --silent http://127.0.0.1:4040/api/tunnels | jq '.tunnels[0].public_url' > .ngrok_ssl_host
rails s puma -b 'ssl://127.0.0.1:3000?key=/home/jess/.ssh/localhost.key&cert=/home/jess/.ssh/localhost.crt'
NGROK_SSL_HOST = File.open('.ngrok_ssl_host').read.match(/[0-9a-z]{12}.ngrok.io/)[0]
config.hosts << NGROK_SSL_HOST
Answered by tidelake on January 15, 2021
Something like:
newhost=$(ngrok http https://localhost:3000 |
grep -E '^Forwardings+https://' |
grep -E -o '[0-9a-f]{12}.ngrok.io`)
if [[ -n "$newhost" ]] ; then
echo "$newhost" >>config.hosts
rails server ...(the rest)
else
echo "Failure in ngrok" >&2
fi
Read man grep bash
.
Answered by waltinator on January 15, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP