This article is part two of a series of articles on GO development on Google Cloud. Read part one here

Now that we've covered the basics of project creation on Google App Engine, it's time to get a REST service up and running on it. In order to accomplish that, we will be using the following tools:

  1. Google Cloud Datastore - Datastore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. The documentation can be found here.
  2. Gin - Gin is an HTTP web framework written in Go (Golang) and features a Martini-like API with much better performance. The documentation can be found here.

The Service

In this post, I’ll be covering all the steps needed to get one resource up and running on GAE. We’ll cover the datastore service creation as well as route handling. The next part of this will cover how to write unit tests for the services. Usually the unit tests should be written before the code, but I want to give a complete perspective of the code before showing how to write the tests.

In order to prevent this post from becoming too long, I’ll just cover one small entity creation called User.

The project structure will be as follows:

Creating the Datastore Wrapper

The very first step is to write the datastore wrapper:

First, we need to define the user package and import some other packages that will be used to persist data on datastore and add log onto AppEngine. Then, we need to define the name of the User index as well as a generic error message. Datastore uses indexes instead of tables, so, think of this index as the User table.

Last but not least, we need to define what the User entity will look like. It will have 2 properties (Name and Email), both strings, and the json: property, which will be the name of those properties when marshalling/parsing the JSON object. The next step is to add the actual code that will handle the requests, and save/retrieve the data on the User index.

This code is a basic CRUD abstraction and is pretty self-explanatory. The only thing we need to keep in mind is that exported methods MUST start with a Capital Character in order to be visible from other packages. If you want to add a private method to this file, all you have to do is add a func with a lowercase first letter.

Creating the Routes & Handlers

I like using Gin to route my services. It’s not mandatory, but it helps simplify the code. We will be adding routes on the main.go file.

AppEngine uses the main() func, and because of that, the entry point for your service needs to be the init() func.

In this file, we’re basically routing all requests to the gin router, and adding handlers for the /users endpoint. You can add middleware to this router object, like authentication, CORS support, and so on. The final code that needs to be added is the one responsible for handling the requests. I’ll add a file user.go in the controllers folder, and the file will be as so:

Pretty straightforward, right? We’re basically parsing the payload JSON to a User object and passing it to the right datastore service method.

Deploying Your App

We just finished writing all the necessary code to get the service running. The project structure should be looking like this:

The app.yaml is pretty much the same as the one used in the first post:

Once again, in order to run your service locally, all you need to do is:

  • dev_appserver.py app.yaml

And to deploy it on your GAE project:

  • gcloud app deploy app.yaml

Conclusion

Datastore provides a powerful no-SQL database with high scalabily and reliabilty and it's quite simple to wrap up on a service. I hope that after reading this post you're excited to write your next REST service on Google AppEngine!


Author

Pedro Costa

Programming isn’t about the language, it’s about the solution. In my 10+ years working in the software industry, I've had great opportunities to work on all types of applications. I've developed a couple of mobile apps and a few Java systems. I've also had the opportunity to write automated tests for a couple of applications. I’m always eager to learn new technologies/methodologies. My motivation is to wake up every day and be better than I was the day before.


Building Accessible Web Applications

READ MORE

Create a Tunnel from Anypoint Platform to GCP Using an HA VPN

READ MORE

Create a Classic Tunnel from GCP to Anypoint Platform

READ MORE

How to Make an Android Splash Screen

READ MORE