More Functions on Google Cloud

Campion
Google Cloud - Community
3 min readMay 9, 2018
Google Cloud

If you haven’t yet read my last article, Deploying a Functions (serverless) app on Google Cloud, you should check it out to give you context for this one.

We’ll assume that:

  • You have the gcloud tool installed
  • You have a project in GCP

Note: you must have the gcloud beta version installed

To install gcloud beta use:

gcloud components update && gcloud components install beta

Recap

Last time, we made a Functions App that looked like this:

exports.helloFunctions = (req, res) => {
res.send('Hello Functions!');
};

It simply sent back Hello Functions! when you hit the endpoint. Now let’s go ahead and expand on that. We’re going to make a text-based adventure game (think Zork).

What Do We Want?

We can think of each step in the adventure as a Function. So, when you start the game, that’s a Function, when you choose to go one way, that’s a Function, or when you choose to go the other way, again, that’s a Function.

So how can we make this work?

Let’s start with an HTML template, which will display a message and two links:

let template = ({ message, opt1, opt2, opt1Label, opt2Label }) => {
return `
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>${message}</p>
<div><a href='${opt1}'>${opt1Label}</a></div>
<div><a href='${opt2}'>${opt2Label}</a></div>
</body>
</html>
`

As we can see here, the template takes in a message, a couple of links, and a couple of labels for those links.

Now, let’s serve up our adventure!

We’ll make a new function, called startAdventure that will welcome our player to the game and give them two options:

exports.startAdventure = (req, res) => {
res.status(200).send(template({
message: `Greetings adventurer... Let us begin.
Are you a knight or an elf?
`,
opt1: req.url + "Knight",
opt2: req.url + "Elf",
opt1Label: "I am a knight, strong and skilled with a sword.",
opt2Label: "An elf, am I, known for my ranged ability."
}));
};

Now let’s deploy the start of our adventure:

gcloud beta functions deploy startAdventure --trigger-http

This deploys the start of our adventure. It should output the URL, but if you miss it the format is:

https://[GCP-REGION]-[PROJECT_ID].cloudfunctions.net/startAdventure

You can open up that webpage and see something like this:

https://[GCP-REGION]-[PROJECT_ID].cloudfunctions.net/startAdventure

However, these links don’t work yet. Let’s write the one for the knight:

exports.Knight = (req, res) => {
res.status(200).send(template({
message: `So you are a Knight..
Where do you want to go?
`,
opt1: req.url + "Castle",
opt2: req.url + "Mountain",
opt1Label: "To the castle! I might talk to the princess.",
opt2Label: "I see a dragon in the distance, to the mountain!"
}));
};

Now let’s deploy this:

gcloud beta functions deploy Knight --trigger-http

So now when you click that first link, you should end up here:

https://[GCP-REGION]-[PROJECT_ID].cloudfunctions.net/Knight

So How Does This Work?

Well, when we made our startAdventure function, we told it to serve an HTML page (the one above) with links to req.url + Knight and req.url + Elf . You’ll notice that when you click on the Knight link, the URL changes, ending with Knight now instead of startAdventure . And we can see that our Knight page links to Castle and Mountain .

What’s Next?

Go ahead and keep going! The possibilities are endless

I encourage you to continue developing your adventure! You can change the template if you want to customize it with more options or even an image.

And the best part?

Functions are 💲🆓.⁹⁹ (FREE)!

Here is the Pricing if you don’t believe me.

Thanks for reading! If you have any questions, comment below! 👋

--

--