core

package module
v0.0.0-...-35a386b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2025 License: GPL-3.0 Imports: 3 Imported by: 0

README

core

Core Backend Skeleton

go run .\run\main.go

Test Login

image

Publish with Local Port

Publikasikan dengang menggunakan Cloudflare Tunnel

winget install --id Cloudflare.cloudflared
cloudflared tunnel --url 127.0.0.1:3000

Generate Token

to generate PRIVATEKEY and PUBLICKEY

Clone this repo:

https://github.com/whatsauth/watoken

run

go test

CI / CD to Google Clound Function

save as file into folder

.github/workflows/main.yml

name: Google Cloud Function Deployment
on:
  push:
    branches:
      - main
jobs:
    Deploy:
      name: Deploy
      runs-on: ubuntu-latest
      steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: GCP Authentication
        id: 'auth'
        uses: 'google-github-actions/auth@v2'
        with:
          credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
      - name: Debug GCP credentials
        env:
          GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
        run: |
          echo "$GOOGLE_APPLICATION_CREDENTIALS" > credentials.json
      - name: 'Set up Cloud SDK'
        uses: 'google-github-actions/setup-gcloud@v2'
      - name: 'Use gcloud CLI'
        run: 'gcloud info'
      - name: 'Deploy a gen 2 cloud function'
        run: |
          gcloud functions deploy gocroot \
            --region=asia-southeast2 \
            --allow-unauthenticated \
            --entry-point=WebHook \
            --gen2 \
            --runtime=go125 \
            --trigger-http \
            --timeout=540s \
            --set-env-vars MONGOSTRING='${{ secrets.MONGOSTRING }}',PRIVATEKEY='${{ secrets.PRIVATEKEY }}',PUBLICKEY='${{ secrets.PUBLICKEY }}'
      - name: 'Cek eksistensi fungsi'
        run: 'gcloud functions describe gocroot --region=asia-southeast2'
      - name: 'Cek log debugging'
        run: 'gcloud functions logs read gocroot --region=asia-southeast2'
      - name: 'Cleaning Artifact Registry'
        run: 'gcloud artifacts repositories delete gcf-artifacts --location=asia-southeast2 --quiet'

hasil improve (eksperimental)

name: Google Cloud Function Deployment

on:
  push:
    branches:
      - main
  workflow_dispatch: # Allow manual triggering

env:
  PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
  FUNCTION_NAME: gocroot
  REGION: asia-southeast2

jobs:
  deploy:
    name: Deploy Cloud Function
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    
    - name: Authenticate to Google Cloud
      id: 'auth'
      uses: 'google-github-actions/auth@v2'
      with:
        credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
        project_id: ${{ secrets.GCP_PROJECT_ID }}
    
    - name: Set up Cloud SDK
      uses: 'google-github-actions/setup-gcloud@v2'
    
    - name: Configure gcloud project
      run: |
        gcloud config set project ${{ env.PROJECT_ID }}
        gcloud config list
    
    - name: Deploy Cloud Function
      run: |
        gcloud functions deploy ${{ env.FUNCTION_NAME }} \
          --region=${{ env.REGION }} \
          --allow-unauthenticated \
          --entry-point=WebHook \
          --gen2 \
          --runtime=go121 \
          --trigger-http \
          --timeout=540s \
          --memory=256Mi \
          --min-instances=0 \
          --max-instances=100 \
          --set-env-vars MONGOSTRING='${{ secrets.MONGOSTRING }}',PRIVATEKEY='${{ secrets.PRIVATEKEY }}',PUBLICKEY='${{ secrets.PUBLICKEY }}' \
          --quiet
    
    - name: Verify deployment
      run: |
        echo "Checking function status..."
        gcloud functions describe ${{ env.FUNCTION_NAME }} --region=${{ env.REGION }} --format="value(status)"
        
        echo "Function URL:"
        gcloud functions describe ${{ env.FUNCTION_NAME }} --region=${{ env.REGION }} --format="value(serviceConfig.uri)"
    
    - name: Run health check
      run: |
        FUNCTION_URL=$(gcloud functions describe ${{ env.FUNCTION_NAME }} --region=${{ env.REGION }} --format="value(serviceConfig.uri)")
        echo "Testing function at: $FUNCTION_URL"
        # Add your health check endpoint here
        # curl -f "$FUNCTION_URL/health" || exit 1
    
    - name: Check recent logs (on failure)
      if: failure()
      run: |
        echo "Checking recent logs for debugging..."
        gcloud functions logs read ${{ env.FUNCTION_NAME }} --region=${{ env.REGION }} --limit=50

  cleanup:
    name: Cleanup Old Artifacts
    runs-on: ubuntu-latest
    needs: deploy
    if: success()
    
    steps:
    - name: Authenticate to Google Cloud
      uses: 'google-github-actions/auth@v2'
      with:
        credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
        project_id: ${{ secrets.GCP_PROJECT_ID }}
    
    - name: Set up Cloud SDK
      uses: 'google-github-actions/setup-gcloud@v2'
    
    - name: Clean up old artifacts
      run: |
        # List repositories first
        gcloud artifacts repositories list --location=${{ env.REGION }}
        
        # Only delete if gcf-artifacts exists and has old images
        if gcloud artifacts repositories describe gcf-artifacts --location=${{ env.REGION }} &>/dev/null; then
          echo "Cleaning up old container images..."
          # Delete images older than 7 days
          gcloud artifacts docker images list ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/gcf-artifacts \
            --filter="createTime<'-P7D'" --format="value(IMAGE)" | \
            xargs -r gcloud artifacts docker images delete --quiet
        else
          echo "No gcf-artifacts repository found, skipping cleanup"
        fi

Database Connection

Untuk koneksi database menggunakan mgdb:

https://github.com/gocroot/mgdb

go get github.com/gocroot/mgdb

dalam config tambahkan variabel untuk menampung koneksi database mongo dengan errornya

var (
    once        sync.Once
    Port        string
    MongoString string
    PrivateKey  string
    PublicKey   string
    
    // Database variables
    MongoDB     *mongo.Database
    //opsional untuk pengecekan error koneksi
    MongoErr    error
)

// SetEnv dengan protection untuk multiple calls
func SetEnv() {
	once.Do(func() {
		// Load environment variables
		// Set default configurations
		// Initialize global settings
		Port = ":3000"
		MongoString = os.Getenv("MONGOSTRING")
		PrivateKey = os.Getenv("PRIVATEKEY")
		PublicKey = os.Getenv("PUBLICKEY")
        var mconn = mgdb.DBInfo{
            DBString: MongoString,
            DBName:   "mydatabase",
        }
        MongoDB, MongoErr = mgdb.MongoConnect(mconn)
        //opsional untuk pengecekan koneksi, bisa dilakukan di luar config
        if MongoErr != nil {
            log.Fatalf("Failed to connect to MongoDB: %v", MongoErr)
        }
	})
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL