Last active
June 4, 2024 14:15
-
-
Save pmorelli92/ee03db5cae03197a48e546c79624af0c to your computer and use it in GitHub Desktop.
Example bunnify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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