This context provides a step-by-step guide on how to create a Go (Golang) API on Google App Engine.
Abstract
The context discusses the benefits of using Google App Engine, including its Platform as a Service (PaaS) nature, scalability, and support for various programming languages. It explains the difference between the standard and flexible environments in App Engine and provides instructions on setting up an App Engine project in the Cloud Console. The context also includes a walkthrough on writing a simple server application in Go, handling query parameters, and deploying the application to App Engine. It concludes with instructions on testing the application.
Bullet points
Google App Engine is a Platform as a Service (PaaS) that offers scalability, high availability, and support for various programming languages.
App Engine has two environments: standard and flexible. The standard environment is suitable for applications that need rapid scaling or experience unexpected traffic spikes, while the flexible environment is ideal for applications with consistent traffic or needing to scale up or down.
The context provides a step-by-step guide on setting up an App Engine project in the Cloud Console, including initializing the project using the gcloud command.
The context includes a walkthrough on writing a simple server application in Go, including creating a function to grab the port number, initializing an HTTP server client, and dealing with query parameters.
The context provides instructions on deploying the application to App Engine using the gcloud app deploy command.
The context concludes with instructions on testing the application, including finding the reserved App Engine URL in the Google Cloud Console and testing the API with query parameters.
Golang
How to Create a Go (Golang) API on Google App Engine
A simple walkthrough on creating an API with a server, written in Go, to deploy to Google App Engine.
Why Google App Engine?
Google App Engine is Platform as a Service (PaaS). It aims to make deployment easier. It is fully managed, pay-as-you-go, high availability, it ensures a fast time to market, and supports a wide variety of programming languages.
App Engine allows you to build scalable web and mobile back-ends with traffic splitting and firewall capabilities.
Standard vs Flexible Environment
You have two options when creating an App Engine environment:
Standard — Applications run in a sandbox. Choose this if you need rapid scaling or you experience dramatic and unexpected spikes of traffic
Flexible — Instancesrun within Docker containers on Compute Engine virtual machines. Choose this if you have consistent traffic, need to scale up or down, or access resources on the Compute Engine.
It’s important to note that the App Engine Application URL will be named based on the project name. So take this into account when choosing your cloud project.
This gives you access to the command line tools (CLI).
Initialize the project
Using the gcloud command, initialize the project.
$ gcloud init
Until you’ve deployed your application to the app engine, your dashboard will look like this.
So now that the app engine is all setup, it’s time to actually write our server.
2) Write a simple Server application in Go
We’ll be using the Go version 1.14. We’ll also be using Go modules.
There are 3 files that are essential to our application. These are outlined below.
Runtime Config — app.yaml
The first file that we need to create is the app.yaml file. This file is mandatory in an app engine project.
Go Module — go.mod
We also need to create our Go module.
Main File — main.go
Firstly, we need to create a function in the main file that grabs the port number. It’s just a function that just gets the appropriate port number that our API is listening on based on the current environment.
We use the os library to get the port (if it is an environment variable), default to port 8080 if the environment variable isn’t set, and return the port number to the caller as a string.
Initialize our HTTP server client
Let’s create our simple HTTP server to listen to requests.
Firstly we’ll get the port using our getPort() function we created previously.
We’ll then write our anonymous handler function to handle requests and deal with the response.
Our API doesn’t do anything yet, so it’s pretty useless. But we’ll soon start handling some parameters like you would in an actual API.
Then we’ll call the function that instructs our HTTP client to listen and serve our application on the specified port with http.ListenAndServe , while catching any errors that might arise.
As you can see, the callback function takes two parameters http.ResponseWriter to write the response to, and *http.Request which is a pointer to the Request object.
The API is a bit too basic at the moment. In the real world, you’d need to handle query parameters to feed our API.
Dealing with query parameters is pretty simple in Go. You just have to remember that, when accessing a query parameter with URL.Query()['value'] , it’ll always return an array of items. So make sure you access the value via array indexing.
Instead of writing an anonymous function as the callback to the HandleFunc function, we’ll provide our new function APIHandler to handle the request along with any parameters that are provided in the URL.
Our APIHandler function has to conform to callback signature so we have our two parameters http.ResponseWriter and *http.Request .
In this example function, we don’t accept POST request, and return early with an HTTP error if that is the case.
We then grab the query parameter value , if there is one. If it is invalid or empty, we return and don’t do anything.
If it exists and is valid, we return the value in the response (for demonstration purposes).
That’s our basic API written…
3) Deploy to App Engine
Running the gcloud init command in the earlier step should have logged you in to your google cloud account. But if not, run gcloud auth login to login.
After this step, you’ll want to deploy your application to the App Engine using
$ gcloud app deploy
This means that your API is successfully deployed to the URL provided.
4) Test the application
Find your reserved app engine URL in the google cloud console.
To test that it's all up and running, all you need to do is click the link to see your working app engine application.
You can see if your application has successfully parsed the query parameters by testing the URL (typing it into the browser).