Skip to content

GeographicInformationSystem/backend

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deplong Golang CI/CD to Google Cloud Platform

This is a simple Golang Model-Controller template using Functions Framework for Go and mongodb.com as the database host. It is compatible with Google Cloud Function CI/CD deployment.

Start here: Just Fork this repo

MongoDB Preparation

The first thing to do is prepare a Mongo database using this template:

  1. Sign up for mongodb.com and create one instance of Data Services of mongodb.

  2. Go to Network Access menu > + ADD IP ADDRESS > ALLOW ACCESS FROM ANYWHERE
    image

  3. Download MongoDB Compass, connect with your mongo string URI from mongodb.com

  4. Create database name iteung and collection reply
    image

  5. Import this json into reply collection.
    image
    image
    image

  6. Create a profile collection, and insert this JSON document with your 30-day token and WhatsApp number.
    image
    image

    {
      "token":"v4.public.asoiduasoijfiun98erjg98egjpoikr",
      "phonenumber":"6281111222333"
    }

    image

Folder Structure

This boilerplate has several folders with different functions, such as:

  • .github: GitHub Action yml configuration.
  • config: all apps configuration like database, API, token.
  • controller: all of the endpoints functions
  • model: all of the type structs used in this app
  • helper: helper folder with a list of functions only called by others file
  • route: all routes URL

GCP Cloud Function CI/CD setup

To get an auth in Google Cloud, you can do the following:

  1. Open Cloud Shell Terminal, type this command line per line:

    image

    # Get a List of Project IDs in Your GCP Account
    gcloud projects list --format="value(projectId)"
    # Set Project ID Variable
    PROJECT_ID=yourprojectid
    # Create a service account
    gcloud iam service-accounts create "whatsauth" --project "${PROJECT_ID}"
    # Create JSON key for GOOGLE_CREDENTIALS variable in GitHub repo
    gcloud iam service-accounts keys create "key.json" --iam-account "whatsauth@${PROJECT_ID}.iam.gserviceaccount.com"
    # Read the key JSON file and copy the output, including the curl bracket, go to step 5.
    cat key.json
    # Authorize service account to act as admin in Cloud Run service
    gcloud projects add-iam-policy-binding ${PROJECT_ID} --member=serviceAccount:whatsauth@${PROJECT_ID}.iam.gserviceaccount.com --role=roles/run.admin
    # Authorize service account to delete artifact registry
    gcloud projects add-iam-policy-binding ${PROJECT_ID} --member=serviceAccount:whatsauth@${PROJECT_ID}.iam.gserviceaccount.com --role=roles/artifactregistry.admin
    # Authorize service account to deploy cloud function
    gcloud projects add-iam-policy-binding ${PROJECT_ID} --member=serviceAccount:whatsauth@${PROJECT_ID}.iam.gserviceaccount.com --role=roles/cloudfunctions.developer
    gcloud projects add-iam-policy-binding ${PROJECT_ID} --member=serviceAccount:whatsauth@${PROJECT_ID}.iam.gserviceaccount.com --role=roles/logging.viewer
  2. Open Menu Cloud Build>settings, select the Service Account created by step 1, and enable Cloud Function Developer.
    image
    image

  3. Go to the GitHub repository; in the settings, menu>secrets>action, add GOOGLE_CREDENTIALS vars with the value from the key.json file.

  4. Add other Vars into the secret>action menu:

    MONGOSTRING=mongodb+srv://user:pass@gocroot.wedrfs.mongodb.net/
    WAQRKEYWORD=yourkeyword
    WEBHOOKURL=https://asia-southeast1-PROJECT_ID.cloudfunctions.net/gocroot/webhook/inbox
    WEBHOOKSECRET=yoursecret
    WAPHONENUMBER=62811111

WhatsAuth Signup

  1. Go to the WhatsAuth signup page and scan with your WhatsApp camera menu for login.
  2. Input the webhook URL(https://yourappname.alwaysdata.net/whatsauth/webhook) and your secret from the WEBHOOKSECRET setting environment on Always Data.
    image
  3. Follow this instruction, at the end of the instruction, you will get 30 days token using this request
  4. Save the token into MongoDB, open iteung db, and insert this JSON document with your 30-day token and WhatsApp number.
    image
    {
      "token":"v4.public.asoiduasoijfiun98erjg98egjpoikr",
      "phonenumber":"6281111222333"
    }
    image

Refresh Whatsapp API Token

To continue using the WhatsAuth service, we must obtain a new token every three weeks before it expires in 30 days.

  1. Open Menu Cloud Scheduler. You can just search it like the screenshot.
    image
  2. Click Create Job Input every 29 days; next, choose Target type HTTP, input refresh token URL from cloud function, HTTP method Get.
    image
    image
  3. Completing create schedule

Upgrade Apps

If you want to upgrade apps, please delete (go.mod) and (go.sum) files first, then type the command in your terminal or cmd :

go mod init gocroot
go mod tidy

Managing Module

Steps are follow:

  1. Create Package in mod folder
  2. Edit modcaller.go file, call your mod package here
  3. Register keyword in module collection in mongodb

About

ini untuk backend pada parkirgratis

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%