This repository contains all the code that we built to develop RestFUL API with go-gin-gorm. In order to replicate this whole project, make sure you are following this guide that we've provided below.
- Requirements
- Setting Up Google Cloud Platform
- Setting Up Golang Environment in GCE
- Deployment and Integration
- API Testing
In order to run through this whole tutorial, make sure you meet these requirements:
- Running desktop/laptop with internet connection
- A Web Browser
- Postman
New Project and Billings
- In your web browser, Go to Google Cloud Platform
- Register and Login with your account.
- Create a new project.
- Create a new billing account and redeem (if you haven't) the free $300 worth of credits.
Creating PostgreSQL Instance
5. On the sidebar of the web, open Cloud SQL.
6. Create a new PostgreSQL instance with the credentials and settings as you wish. Then, create a new db called inscure
7. For your information, this instance will store all of the tables that we define in /entity
directory.
Creating Cloud Storage Bucket
8. On the sidebar of the web, open Cloud Storage -> Buckets.
9. Create a new bucket with the credentials and settings as you wish.
10. Inside that bucket, create 2 folders called prediction
and profile_picture
.
11. Upload a single file in the root directory for user default profile picture and rename it to default.png
12. Its pretty self explanatory to know the purpose of this bucket by doing this.
Creating Compute Engine Instance 13. On the sidebar of the web,
- open IAM -> Service Account and create new account
- open VPC -> Firewall and create a new firewall that allow tcp port 8080 with target name as you wish.
- Go to Compute Engine -> VM Instances then Create a new instance with the credentials and settings as you wish.
- make sure to use the firewall with the specified target-name.
- After the process has finished, set a Reserve Static IP for the instance then connect to it via SSH.
- Inside the SSH, run
sudo apt-get update
, then install golang and pm2. - For go installation please refer to this doc
- For pm2 installation,
- Install nvm and nodeJS by referring to this doc
- Install pm2 by entering
npm i pm2 -g
- Inside your root directory, clone this repository by using
git clone <repo-url>
.
- Navigate to
/inscure-fe
directory, then enternano .env
. Copy and fill this snippet based on your postgre instance,
DB_HOST=<your postgre public ip>
DB_USER=postgres
DB_PASS=<your instance password>
DB_NAME=inscure
DB_PORT=5432
- Save and Close the file.
- Go through all the files inside
/service
and/utils
directory, then replace all strings containingexample-bucket-test-cc-trw
with your bucket name.
- Navigate to
/inscure-fe
directory, and enternano package.json
then copy this snippet inside it.
{
"apps": [
{
"name": "main",
"script": "go run main.go"
}
]
}
- Save and close the file.
- Inside the same directory, run
pm2 start package.json
- Voila! You've managed to deploy our RestFUL API by with GCP.
To test the API, copy the external IP address of your compute engine instance, then refer to this link for available api endpoints Postman Documentation