Skip to content

Instantly share code, notes, and snippets.

@pmorelli92
Last active April 13, 2023 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmorelli92/ee03db5cae03197a48e546c79624af0c to your computer and use it in GitHub Desktop.
Save pmorelli92/ee03db5cae03197a48e546c79624af0c to your computer and use it in GitHub Desktop.
Example bunnify
package main
import (
"context"
"time"
"fmt"
"github.com/pmorelli92/bunnify/bunnify"
)
func main() {
c := bunnify.NewConnection()
c.Start()
c.NewQueueListener(
"exchange1",
"queue1",
bunnify.NewHandlerFor("catCreated", HandleCatCreated),
bunnify.NewHandlerFor("personCreated", HandlePersonCreated)).Listen()
publisher := c.NewPublisher()
err := publisher.Publish(context.Background(), "exchange1", "catCreated", bunnify.PublishableEvent{
Metadata: bunnify.Metadata{
ID: "IDFROMCODE",
CorrelationID: "CORFORCODE",
Timestamp: time.Now(),
},
Payload: catCreated{
Years: "22",
},
})
if err != nil {
panic(err)
}
}
type personCreated struct {
Name string `json:"name"`
}
func HandlePersonCreated(ctx context.Context, event bunnify.ConsumableEvent[personCreated]) error {
fmt.Println("Event ID: " + event.ID)
fmt.Println("Correlation ID: " + event.CorrelationID)
fmt.Println("Timestamp: " + event.Timestamp.Format(time.DateTime))
fmt.Println("Person created with name: " + event.Payload.Name)
return nil
}
type catCreated struct {
Years string `json:"years"`
}
func HandleCatCreated(ctx context.Context, event bunnify.ConsumableEvent[catCreated]) error {
fmt.Println("Event ID: " + event.ID)
fmt.Println("Correlation ID: " + event.CorrelationID)
fmt.Println("Timestamp: " + event.Timestamp.Format(time.DateTime))
fmt.Println("Cat created with years: " + event.Payload.Years)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment