← Back to Blog

Deploying a Golang REST API on BizflyCloud App Engine

Back with the BizflyCloud App Engine Tutorials Series, a series of posts walking through deploying all kinds of applications written in different languages onto the BizflyCloud App Engine platform.

If you're a developer, whether Backend or Frontend, you've definitely worked with APIs quite a bit. In today's post I'll walk you through deploying a REST API written in Golang. After this post, I hope you'll know about another new platform that lets you deploy applications quickly and conveniently. No more wasting time, let's deploy this application together.

Prerequisites

To follow along with this post, you'll need a few things:

  • A BizflyCloud account — if you don't have one yet, you can create one right here. Creating an account only takes a few minutes. The App Engine service currently gives you free resources up to 8 CPU-16GB Memory so you can comfortably deploy all kinds of applications.
  • Any Golang code repo.

Source code

In this post I'll borrow code from a Repo I found on GitHub:

github.com/PhongVX/golang-rest-api

Let me explain a bit about its functionality — this application lets you:

  • Create a User
  • Update a User
  • Delete a User
  • Update User info
  • Get info for 1 or all Users

Fairly simple, right 😁. You can try deploying this application directly, or deploy your own Golang application!

This application listens on port 5000 when running — this is the value we need to note down to fill in when creating the service. If you want to understand the source code more, you can read the post attached to the repo.

Creating an App

Before you want to deploy an application, you need to create an App first — this App's purpose is to contain related applications together. Here I've already created an App named example, so I'll just use that.

Creating a Service

The Service will be your application running on App Engine. To deploy the Golang application, we choose Create Service.

Next we fill in a few fields needed to deploy the application.

  • Service name: The name of the Service, for reference
  • Location: The location where you want to deploy the application.
  • Source type: You can choose to deploy the application from a Git source or an Image.
  • Source URL: Since we're deploying via git, this will be the link to the repo containing the source code.
  • Branch: The repo branch you want to deploy. You can deploy multiple versions of an application (dev, staging, production) using this feature.
  • Config: The infrastructure config running the application.
  • Replicas: The number of copies — the more copies, the bigger the infrastructure and the higher the stability.
  • Port: The port the application will run on. This is the value I noted above.
  • Application type: Since our application runs the HTTP protocol, I'll choose the HTTP application type

Once you've filled in a few fields like above, you can deploy the application right away by choosing Confirm service info

The application build process gets deployed and continuously updated immediately

Once deployed, the system returns a URL for you to access the deployed application, as shown:

example-nnwpu.appengine.bfcplatform.vn/go-rest-api

How does the system build the application?

BizflyCloud's App Engine uses Buildpack technology to package your application based on specific files — to understand how your application gets packaged, you can check the Build Logs in the Logs section in the left-side menu. This Golang application's Build Logs will look like this:

[2023-01-25 15:43:04.401 ][ INFO ]: ===> ANALYZING
[2023-01-25 15:43:04.425 ][ INFO ]: Previous image with name "**********************************/****************************/go-rest-api-nnwpu:latest" not found
[2023-01-25 15:43:04.435 ][ INFO ]: ===> DETECTING
[2023-01-25 15:43:04.493 ][ INFO ]: 1 of 2 buildpacks participating
[2023-01-25 15:43:04.505 ][ INFO ]: heroku/go 0.0.0
[2023-01-25 15:43:04.514 ][ INFO ]: ===> RESTORING
[2023-01-25 15:43:04.521 ][ INFO ]: ===> BUILDING
[2023-01-25 15:43:16.960 ][ INFO ]: -----> Fetching jq... done
[2023-01-25 15:43:17.996 ][ INFO ]: -----> Fetching stdlib.sh.v8... done
[2023-01-25 15:43:18.070 ][ INFO ]: ----->
[2023-01-25 15:43:18.083 ][ INFO ]: Detected go modules via go.mod
[2023-01-25 15:43:18.094 ][ INFO ]: ----->
[2023-01-25 15:43:18.106 ][ INFO ]: Detected Module Name: github.com/PhongVX/golang-rest-api
[2023-01-25 15:43:18.115 ][ INFO ]: ----->
[2023-01-25 15:43:18.123 ][ INFO ]: -----> New Go Version, clearing old cache
[2023-01-25 15:43:18.139 ][ INFO ]: -----> Installing go1.12.17
[2023-01-25 15:43:39.520 ][ INFO ]: -----> Fetching go1.12.17.linux-amd64.tar.gz... done
[2023-01-25 15:43:39.641 ][ INFO ]: -----> Determining packages to install
[2023-01-25 15:43:39.673 ][ INFO ]: go: finding github.com/gorilla/mux v1.7.3
[2023-01-25 15:43:45.039 ][ INFO ]: go: downloading github.com/gorilla/mux v1.7.3
[2023-01-25 15:43:45.091 ][ INFO ]: go: extracting github.com/gorilla/mux v1.7.3
[2023-01-25 15:43:45.123 ][ INFO ]:
[2023-01-25 15:43:45.133 ][ INFO ]: Detected the following main packages to install:
[2023-01-25 15:43:45.141 ][ INFO ]: github.com/PhongVX/golang-rest-api
[2023-01-25 15:43:45.149 ][ INFO ]:
[2023-01-25 15:43:45.156 ][ INFO ]: -----> Running: go install -v -tags heroku github.com/PhongVX/golang-rest-api
[2023-01-25 15:43:45.287 ][ INFO ]: github.com/PhongVX/golang-rest-api/entities
[2023-01-25 15:43:45.298 ][ INFO ]: github.com/gorilla/mux
[2023-01-25 15:43:45.306 ][ INFO ]: github.com/PhongVX/golang-rest-api/models
[2023-01-25 15:43:45.318 ][ INFO ]: github.com/PhongVX/golang-rest-api/apis/userapi
[2023-01-25 15:43:45.410 ][ INFO ]: github.com/PhongVX/golang-rest-api
[2023-01-25 15:43:46.416 ][ INFO ]:
[2023-01-25 15:43:46.426 ][ INFO ]: Installed the following binaries:
[2023-01-25 15:43:46.435 ][ INFO ]: ./bin/golang-rest-api
[2023-01-25 15:43:46.472 ][ INFO ]:
[2023-01-25 15:43:46.482 ][ INFO ]: Created a Procfile with the following entries:
[2023-01-25 15:43:46.491 ][ INFO ]: web: bin/golang-rest-api
[2023-01-25 15:43:46.500 ][ INFO ]:
[2023-01-25 15:43:46.508 ][ INFO ]: If these entries look incomplete or incorrect please create a Procfile with the required entries.
[2023-01-25 15:43:46.516 ][ INFO ]: See https://devcenter.heroku.com/articles/procfile for more details about Procfiles
[2023-01-25 15:43:46.523 ][ INFO ]:
[2023-01-25 15:43:46.531 ][ INFO ]: ===> EXPORTING
[2023-01-25 15:43:46.538 ][ INFO ]: Adding layer 'heroku/go:profile'
[2023-01-25 15:43:46.546 ][ INFO ]: Adding layer 'launch.sbom'
[2023-01-25 15:43:46.570 ][ INFO ]: Adding 1/1 app layer(s)
[2023-01-25 15:43:46.580 ][ INFO ]: Adding layer 'launcher'
[2023-01-25 15:43:46.588 ][ INFO ]: Adding layer 'config'
[2023-01-25 15:43:46.596 ][ INFO ]: Adding layer 'process-types'

There are the following steps: ANALYZING => DETECTING => RESTORING => BUILDING => EXPORTING. Basically, these steps detect the application's language and issue the appropriate commands to install dependencies, packages, or pick the right Runtime version.

For a Golang application, the system looks for and reads the go.mod file to configure the system appropriately to run the application, during the DETECTING step

Content of the go.mod file:

module github.com/PhongVX/golang-rest-api

go 1.12

require github.com/gorilla/mux v1.7.3 // indirect

Then in the BUILDING step, the system runs commands to install the module (github.com/PhongVX/golang-rest-api), the package (github.com/gorilla/mux v1.7.3), and picks Golang version 1.12.

Trying it out

So we've successfully deployed the Golang application and understand how it gets built on BFC App Engine. Now let's test whether the functionality actually works. You can use any tool to send a request to the server, like Postman, a browser, Curl, etc. — I'll walk through using curl.

Creating a user

curl -X POST https://example-nnwpu.appengine.bfcplatform.vn/go-rest-api/api/v1/user/create \
-H "Content-Type: application/json" \
-d '{"id": "001","name":"BFC-User","password":"123456"}'

Getting all users

curl https://example-nnwpu.appengine.bfcplatform.vn/go-rest-api/api/v1/user/getall

The returned response has this content:

[{"id":"001","name":"BFC-User","password":"123456"}]

So the functionality works as expected 😅. You can try the other functions too: delete, update, etc.

So we've successfully deployed a Golang Rest API application!

Wrapping up

The BFC App Engine platform was born to simplify the application deployment process for developers — this platform is still under development, and I hope you'll try it out and give me feedback on the product. If you found this post good and want to catch more of my posts, please Upvote and Follow me. Thanks for taking the time to read this post, see you in the next one.

Oh, and Happy New Year 🎆🎆🎆. Wishing you all a new year with everything going your way, bug-free code, and a system that never goes down 😁

Have thoughts on this?

I'd love to hear your perspective. Send me a message.

Get in touch