Route your action in gcp Pubsub easy
go get github.com/sofyan48/pubsub-router
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)
}
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
}
}
// add router
rtr := pubsubrouter.NewRouter()
// setup routing and handler
rtr.Handle("/event", handlerMessage())
rtr.Handle("/test", handlerMessage2())
sv.Subscribe(os.Getenv("EVENT_BROKER_SERIAL"), rtr).Start()
Send event with attribute path
sv.Publish(os.Getenv("EVENT_BROKER_SERIAL"), "/test", "Message send test")
For best model example check example folder
go run example/main.go