Skip to content

Commit

Permalink
Merge pull request #233 from TykTechnologies/ngrok-refactor
Browse files Browse the repository at this point in the history
Ngrok refactor
  • Loading branch information
davegarvey authored Dec 21, 2023
2 parents 4d9d4ed + c724823 commit 93e02ee
Show file tree
Hide file tree
Showing 6 changed files with 618 additions and 54 deletions.
5 changes: 3 additions & 2 deletions deployments/tyk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,14 @@ The ngrok deployment contains a dashboard which records all requests which pass

- [ngrok dashboard](http://localhost:4040)

**Important**: Ngrok requires an auth token. You must provide your own token by creating an Ngrok account via their website. Once you have an auth token (note, "auth token", not "api key", ngrok has both), add it to the Tyk Demo `.env` file as `NGROK_AUTHTOKEN` e.g. `NGROK_AUTHTOKEN=MY-AUTH-TOKEN-123`.

#### Usage

The Ngrok tunnel URL is displayed in the output of the bootstrap script (`./up.sh`).
The URL will be something that looks like this: `http://11e3-103-252-202-110.ngrok.io`

APIs can be accessed through the tunnel URL using the same paths as they are accessed through the Gateway URL.
For example, using the example tunnel URL provided above, the Basic Open API can be accessed as follows:
APIs can be accessed through the tunnel URL using the same paths as they are accessed through the Gateway URL. For example, using the example tunnel URL provided above, the Basic Open API can be accessed as follows:

- Gateway URL: http://tyk-gateway.localhost:8080/basic-open-api/get
- External Tunnel URL: http://11e3-103-252-202-110.ngrok.io/basic-open-api/get
Expand Down
48 changes: 32 additions & 16 deletions deployments/tyk/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ bootstrap_progress

log_message "Copying Python bundle to http-server"
# we don't use a 'docker compose' command here as docker compose version 1 does not support 'cp'
docker cp $(get_service_container_id tyk-gateway):/opt/tyk-gateway/middleware/python/basic-example/bundle.zip deployments/tyk/volumes/http-server/python-basic-example.zip 2>>logs/bootstrap.log
docker cp $(get_service_container_id tyk-gateway):/opt/tyk-gateway/middleware/python/basic-example/bundle.zip deployments/tyk/volumes/http-server/python-basic-example.zip 2>> logs/bootstrap.log
if [ "$?" != 0 ]; then
echo "Error occurred when copying Python bundle to http-server"
exit 1
Expand Down Expand Up @@ -478,21 +478,34 @@ fi
log_ok
bootstrap_progress

log_message "Getting ngrok public URL for Tyk Gateway"
ngrok_dashboard_url="http://localhost:4040"
ngrok_ip_api_endpoint="$ngrok_dashboard_url/api/tunnels/tyk-gateway"
log_message " Getting data from $ngrok_ip_api_endpoint"
ngrok_public_url=$(curl --fail --silent --show-error ${ngrok_ip_api_endpoint} | jq ".public_url" --raw-output)
if [ "$?" != 0 ]; then
echo "Error getting ngrok configuration from $ngrok_ip_api_endpoint"
exit 1
fi
if [ "$ngrok_public_url" = "" ]; then
echo "Error: ngrok public URL is empty"
exit 1
# Ngrok

ngrok_available=false
if ! grep -q "NGROK_AUTHTOKEN=" .env; then
log_message "Ngrok auth token is not set, so Ngrok will not be available"
log_message "To enable Ngrok, set the NGROK_AUTHTOKEN value in the Tyk Demo .env file"
else
log_message "Getting Ngrok public URL for Tyk Gateway"
ngrok_dashboard_url="http://localhost:4040"
ngrok_ip_api_endpoint="$ngrok_dashboard_url/api/tunnels/tyk-gateway"
log_message " Getting data from $ngrok_ip_api_endpoint"
ngrok_public_url=$(curl -s --show-error ${ngrok_ip_api_endpoint} 2>> logs/bootstrap.log | jq ".public_url" --raw-output)

# we want to handle ngrok failure gracefully, such that it doesn't prevent the bootstrap from completing
if [ "$?" != 0 ]; then
log_message " ERROR: Unable to get Ngrok configuration from $ngrok_ip_api_endpoint"
ngrok_public_url="not configured"
else
if [ "$ngrok_public_url" = "" ]; then
log_message " ERROR: The Ngrok public URL is empty"
ngrok_public_url="not configured"
else
log_message " Ngrok public URL: $ngrok_public_url"
ngrok_available=true
log_ok
fi
fi
fi
log_message " Ngrok public URL: $ngrok_public_url"
log_ok

log_end_deployment

Expand Down Expand Up @@ -547,7 +560,10 @@ echo -e "\033[2K
▽ Gateway 2 ($(get_service_image_tag "tyk-gateway-2"))
URL : $gateway2_base_url
Gateway API Header : x-tyk-authorization
Gateway API Key : $gateway2_api_credentials
Gateway API Key : $gateway2_api_credentials"
if [ "$ngrok_available" = "true" ]; then
echo -e "
▽ Ngrok
Public URL : $ngrok_public_url
Dashboard URL : $ngrok_dashboard_url"
fi
Loading

0 comments on commit 93e02ee

Please sign in to comment.