Skip to content

sofyan48/pubsub-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GCP PubSub Router

Pubsub Router Local Development

Route your action in gcp Pubsub easy

Installing

go get github.com/sofyan48/pubsub-router

Setup Client

package main

import (
	"context"
	"fmt"
	"os"

	"cloud.google.com/go/pubsub"
	"github.com/joho/godotenv"
	pubsubrouter "github.com/sofyan48/pubsub-router"
)

func main() {
	err := godotenv.Load()
	if err != nil {
		fmt.Println("error: ", err)
	}
	cfg := &pubsubrouter.Config{
		Type:                    "service_account",
		ProjectID:               os.Getenv("GOOGLE_PROJECT_ID"),
		PrivateKeyID:            os.Getenv("GOOGLE_PRIVATE_KEY_ID"),
		PrivateKey:              os.Getenv("GOOGLE_PRIVATE_KEY"),
		ClientEmail:             os.Getenv("GOOGLE_CLIENT_EMAIL"),
		ClientID:                os.Getenv("GOOGLE_CLIENT_ID"),
		AuthURI:                 os.Getenv("GOOGLE_AUTH_URI"),
		TokenURI:                os.Getenv("GOOGLE_TOKEN_URI"),
		AuthProviderX509CertURL: os.Getenv("GOOGLE_AUTH_PROVIDER"),
		ClientX509CertURL:       os.Getenv("GOOGLE_CLIENT_CERT_URL"),
	}
	sv := pubsubrouter.NewServer(context.Background(), cfg)
}

Setup Handler

func handlerMessage() HandlerFunc {
	return func(m *pubsubrouter.Message) error error {
		fmt.Println("FROM EVENT:> ", string(m.Data))
		return nil
	}
}

func handlerMessage2() HandlerFunc {
	return func(m *pubsubrouter.Message) error {
		fmt.Println("FROM TEST:> ", string(m.Data))
		return nil
	}
}

Setup Router and Handler

	// add router
	rtr := pubsubrouter.NewRouter()
	
	// setup routing and handler
	rtr.Handle("/event", handlerMessage())
	rtr.Handle("/test", handlerMessage2())

Starting Subscriber

	sv.Subscribe(os.Getenv("EVENT_BROKER_SERIAL"), rtr).Start()

Starting Publisher

Send event with attribute path

	sv.Publish(os.Getenv("EVENT_BROKER_SERIAL"), "/test", "Message send test")

Example

For best model example check example folder

go run example/main.go