Create a travel list app with Go, Fiber, Angular, MongoDB and Google Cloud Secret Manager

Introduction

I have been writing app service/REST/RPC in Go on and off quite awhile, for freelancing and side projects. I have use numbers of frameworks/libraries like Echo, Gorilla toolkit and so on. Unfortunately, I did not use Go at work. Go is not popular in my region. I write web and Express Node.js at work. Recently I discovered Fiber, a new framework inspired by Express

The API is almost identical to Express, and it feels familiar and less context switching if you were coming from Express or Node.js based background. It lowers the barrier for people who have Javascript background and interested in learning Go. I decided to give it a try by building a mini travel list web app.

The tech stack will be

  • Go (Fiber)
  • MongoDB (MongoDB Atlas)
  • Angular (Not the main focus here, could be any web framework)
  • Google Cloud

The source code is available at Github. The code is written with Clean Architecture pattern. API and static web are served together with Fiber. This approach might not suitable for your use case, but it fits well for this mini travel list app as it can bundle together as one service.

Feel free to create PR if you have any interesting features to add. I will try to keep this repo minimum. Ultimately this repo serves as reference or template repo to get started

Preview

Live preview at travel.yongweilun.me

Updated: Live preview server was removed

Screenshot 2020-04-25 at 5.05.41 PM.png

Requirements

  • Setup MongoDB Atlas, more info on how to setup can find it developer.mongodb.com (optional if just to run locally)
  • Setup Google Cloud project (optional if just to run locally)

Write code

1. Clone project source code

The source code is too long to paste here. Kindly refer to the repo at Github.

2. create .env.yaml

After you created cluster on MongoDB Atlas, you should able to get the connection string. Remember to add .env.yaml to .gitignore

DATABASE_URI: mongodb+srv://USERNAME:PASSWORD@CLUSTER/DATABASE?retryWrites=true&w=majority
DATABASE_NAME: travels
TRAVEL_COLLECTION: travel-list

3. Setup Secret Manager

This section can be skipped if you just want to run locally. This is important as I noticed a lot of engineers didn't handle secrets like database credentials correctly. Learn how to setup from cloud.google.com. The idea is to consume secrets from Secret Manager when APP_ENV is production, to avoid any additional charges, the price of Secret Manager is relatively cheap though. Pricing list can be found here. But you can always consume secrets from Secret Manager and skip the .env.yaml setup, depends on use case.

To create new secret, open console.cloud.google.com, navigate to Security -> Secret Manager. The secret value should be the value from .env.yaml as shown in example below Screenshot 2020-04-25 at 3.59.49 PM.png

After that, create service account, grant with Secret Manager Secret Accessor role, save as secret.json and put at credentials/secret.json. Refer to cloud.google.com for more details.

Copy the Resource ID from Console, and set as environmental variable

APP_SECRET_MANAGER_RESOURCE="projects/PROJECT_ID/secrets/SECRET_NAME/versions/SECRET_VERSION"

4. Run the project

cd travel-list
make install
make start

Open browser at localhost:4000

Next section

This app will be used as the foundation of my Google Cloud series. In the coming sections, I will use this app and setup deployment to Cloud Run/GKE with CI/CD.

Comments (1)