Hot URL mapping using cloud functions

Google Cloud Functions

Cloud Functions from google expose a JS function at a URL endpoint.

Background

We have a WordPress site (http://content.wellzesta.com) that contains daily devotional entries as part of “spiritual wellness”. Each article contains at least one hyperlink to a website (or web app) that displays the bible verse. We began by linking out to biblewebapp.com (http://biblewebapp.com/study/), an open source bible web app. To get to the correct book/chapter/verse, one includes them in a query string. So, to get to get to John 3:16, one appends the query string “?v1=JN3_16” to the base URL to get http://biblewebapp.com/study/?v1=JN3_16

Initial Solution

During testing of the biblewebapp.com site I found bugs in the scrolling and loss of synchronization between the text and the voice, so I decided to arrange things so that, when the time came, we could easily switch to another bible web app. We had access to an Apache web server through a legacy cpanel hosting account at godaddy, so I created the subdomain bible.wellzesta.com and edited the .htaccess file (initialization configuration file for an Apache web server) to do a 301 redirect. Example:

http://bible.wellzesta.com/?v1=JN3_16 -> http://biblewebapp.com/study/?v1=JN3_16

Essentially, the URL bible.wellzesta.com was a very basic cloud-based abstraction layer on top of some bible web app.

Eventually, the bugs in biblewebapp became too much to bear.

After a short search, I found two free bible web apps

  • bible.is
  • bible.com

Like biblewebapp.com, bible.is and bible.com provide bible verse access through a query string. But, the format is different than for biblewebapp.com. Table 1 summarizes the query string formatting for the different apps.

Table 1. Query string format for different bible web apps.

Intermediate Solution

To access the new bible web apps I needed a query string adapter. So I wrote a JS script, wrapped it with express.js and deployed on a “free tier” node.js/express stack instance on Google App Engine. The free tier isn’t actually free, nor is it a free trial. Rather, Google gives you a service credit — a credit towards future services. The compute instance that we got was way more firepower than we need to run a simple function (in memory), so I burned through the credit quite quickly. I needed a better, more sustainable, option — one that was cheaper and more lightweight.

The node.js/express server was doing just one thing. It was running a single function. This function processed GET requests at a URL, extracted a query string and sent a properly formatted 301 redirect. I needed a cloud function.

Final Solution

Welcome google cloud functions (GCF). GCF are JS functions that are run on a cloud-hosted Node.js engine. This gave me exactly what I needed except one thing: Google does not allow you to associate a custom domain name with a cloud function (like they do for Apps on Google App Engine). One gets the following redirect:

https://us-central1-bible-url-switch.cloudfunctions.net/bibleVerse/?v1=JN3_16 -> https://www.bible.com/bible/59/JHN.3.16

This wasn’t quite good enough since all of our links are through bible.wellzesta.com. We need the following:

http://bible.wellzesta.com/ -> us-central1-bible-url-switch.cloudfunctions.net/bibleVerse/

Solution: Let CloudFlare DNS take care of the domain name mapping

Attempt #1

For the wellzesta.com domain, just set a CNAME for bible -> us-central1-bible-url-switch.cloudfunctions.net/bibleVerse

This was unsuccessful since CloudFlare flattens the target domain name. In other words, it can’t deal with the ‘/bibleVerse” part of the URL.

Attempt #2

Use CloudFlare page rules with the following mapping:

*bible.wellzesta.com/* -> https://us-central1-bible-url-switch.cloudfunctions.net/bibleVerse/$2

Works! Note: you need to have a CNAME entry for bible.wellzesta.com otherwise CloudFlare will not know it needs to listen for bible.wellzesta.com DNS requests.

Try it for yourself:

Data

Server Log
Resource utilization for the past 7 days.

Update

[July 2018] Our bill from Google for the cloud function averages $0.12/mo.

--

--