Skip to content

Commit

Permalink
Updated functionality and Readme
Browse files Browse the repository at this point in the history
 * Updated Readme with full info
 * Updated grab-ngrok.js to use JSON.parse instead of Regex
 * Added better error catching for if Ngrok is not running properly
 * Added success message with new routing URL
  • Loading branch information
joshuatz committed Nov 28, 2019
1 parent af75ab8 commit a06b5e3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# gcp-proxy-func
Very simple Google Cloud Function that proxies requests using Express and `http-proxy-middleware`. You define the address that requests should be proxied to by filling out config.js (copy `config.example.js` to `config.js`, and then change the URL).

# Using
Fill in `config.js`, and then either zip up the files and upload via Gcloud admin, or use [the Gcloud CLI](https://cloud.google.com/sdk/gcloud/) and `npm run deploy`. Or see below if using this to proxy to Ngrok.

# Ngrok
If you want to use this to have a stable Google Cloud Function address that proxies requests to a dynamic Ngrok address ([see this blog post for details](https://joshuatz.com/posts/2019/using-google-cloud-functions-permanent-url-to-proxy-ngrok-requests/)), you can automatically redeploy the function with the correct Ngrok public URL after it has changed, by using `npm run ngrok-deploy`.

This command will grab the Ngrok public URL via the localhost API, update config.js by using `fs` to write to the file, and then redeploy your GCloud function to point to the updated URL.
30 changes: 20 additions & 10 deletions grab-ngrok.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,37 @@
const fs = require('fs');
const http = require('http');

const ngrokErr = new Error('Could not parse Ngrok response. Is Ngrok running? Is port 4040 being blocked?');

// Query ngrok local API
http.get('http://127.0.0.1:4040/api/tunnels', (res) => {
const req = http.get('http://127.0.0.1:4040/api/tunnels', (res) => {

let data = '';
res.on('data', (chunk) => {
console.log(chunk);
data += chunk;
});

res.on('end', () => {
// Extract public URL
const publicUrl = /public_url":\s*"(https[^"]+)/.exec(data)[1];

// Update config.js file
const configText = `
try {
const ngrokTunnelInfo = JSON.parse(data);
// Assume first tunnel
const publicUrl = ngrokTunnelInfo.tunnels[0].public_url;
// Update config.js file
const configText = `
module.exports = {
destination: '${publicUrl}'
}
`;
fs.writeFileSync('./config.js', configText);
console.log('config updated!');
return true;
fs.writeFileSync('./config.js', configText);
console.log('config updated!');
return publicUrl;
} catch (e) {
throw ngrokErr;
}
});
});
});

req.on('error', (err) => {
throw ngrokErr;
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"ngrok-deploy": "node grab-ngrok.js && npm run deploy",
"deploy": "gcloud functions deploy proxy --runtime nodejs8 --trigger-http --memory=128 --timeout=60s"
"deploy": "gcloud functions deploy proxy --runtime nodejs8 --trigger-http --memory=128 --timeout=60s && node success-msg.js"
},
"keywords": [],
"author": "Joshua Tzucker",
Expand Down
3 changes: 3 additions & 0 deletions success-msg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @ts-check
const config = require('./config');
console.log(`Success! GCloud config updated to route traffic to ${config.destination}`);

0 comments on commit a06b5e3

Please sign in to comment.