https://github.com/raezil/goeventbus
Event sourcing library in Go
https://github.com/raezil/goeventbus
event-driven eventsourcing golang library rabbitmq
Last synced: 6 months ago
JSON representation
Event sourcing library in Go
- Host: GitHub
- URL: https://github.com/raezil/goeventbus
- Owner: Raezil
- License: mit
- Created: 2024-08-29T13:13:20.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-04T11:19:46.000Z (7 months ago)
- Last Synced: 2025-04-04T12:25:02.146Z (7 months ago)
- Topics: event-driven, eventsourcing, golang, library, rabbitmq
- Language: Go
- Homepage:
- Size: 224 KB
- Stars: 3
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
![]()
[](https://goreportcard.com/report/github.com/Raezil/GoEventBus)
[](https://libs.tech/project/849363374/goeventbus)EventBus is a simple and efficient **event-driven system** for Go applications.
It allows you to **publish** and **subscribe** to events seamlessly.---
## 📦 Installation
To install the library, run:
```sh
go get github.com/Raezil/GoEventBus
```---
## 🚀 Quick Start
### 1️⃣ Initialize a New Project
```sh
mkdir eventbus-demo
cd eventbus-demo
go mod init eventbus-demo
```### 2️⃣ Create `main.go`
```go
package mainimport (
"encoding/json"
"fmt"
"log"
"net/http"gbus "github.com/Raezil/GoEventBus"
"github.com/gorilla/mux"
)// HouseWasSold represents an event for a house sale.
type HouseWasSold struct{}// NewDispatcher sets up event handlers.
func NewDispatcher() *gbus.Dispatcher {
return &gbus.Dispatcher{
HouseWasSold{}: func(data map[string]interface{}) (gbus.Result, error) {
price, ok := data["price"].(int)
if !ok {
return gbus.Result{}, fmt.Errorf("invalid or missing 'price'")
}
message := fmt.Sprintf("House sold for %d!", price)
log.Println(message)
return gbus.Result{Message: message}, nil
},
}
}func main() {
dispatcher := NewDispatcher()
eventStore := gbus.NewEventStore(dispatcher)router := mux.NewRouter()
router.HandleFunc("/house-sold", func(w http.ResponseWriter, r *http.Request) {
eventStore.Publish(gbus.NewEvent(HouseWasSold{}, map[string]interface{}{"price": 100}))
if err := eventStore.Broadcast(); err != nil {
log.Printf("Error broadcasting event: %v", err)
http.Error(w, "Event processing failed", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "House sold event published"})
})log.Println("Server running on :8080")
log.Fatal(http.ListenAndServe(":8080", router))
}
```### 3️⃣ Run Your Application
```sh
go run main.go
```Now, visiting `http://localhost:8080/house-sold` will trigger the event and process it.
---
## 🐇 RabbitMQ Integration
### 1️⃣ Start RabbitMQ
```sh
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:4.0-management
```### 2️⃣ Implement Event Publishing in `main.go`
```go
package mainimport (
"fmt"
"log"
. "github.com/Raezil/GoEventBus"
)func NewDispatcher() *RabbitDispatcher {
return &RabbitDispatcher{
"HouseWasSold": func(data map[string]interface{}) (Result, error) {
price, ok := data["price"].(float64)
if !ok {
return Result{}, fmt.Errorf("invalid or missing 'price'")
}
return Result{Message: fmt.Sprintf("House sold for %.2f", price)}, nil
},
}
}func main() {
dispatcher := NewDispatcher()
rabbitStore, err := NewRabbitEventStore(dispatcher, "amqp://guest:guest@localhost:5672/", "events_queue")
if err != nil {
log.Fatalf("Failed to initialize RabbitEventStore: %v", err)
}rabbitStore.Publish(&Event{Id: "12345", Projection: "HouseWasSold", Args: map[string]interface{}{"price": 100.0}})
rabbitStore.Publish(&Event{Id: "123456", Projection: "HouseWasSold", Args: map[string]interface{}{"price": 200.0}})go rabbitStore.Broadcast()
select {}
}
```### 3️⃣ Run Your Application
```sh
go run main.go
```---
## **📜 Contributing**
Want to improve GoEventBus? 🚀
1. Fork the repo
2. Create a feature branch (`git checkout -b feature-new`)
3. Commit your changes (`git commit -m "Added feature"`)
4. Push to your branch (`git push origin feature-new`)
5. Submit a PR!## 📖 References
- [GoEventBus GitHub Repository](https://github.com/Raezil/GoEventBus)
- [RabbitMQ Official Documentation](https://www.rabbitmq.com/)
- [Event Sourcing Overview](https://martinfowler.com/eaaDev/EventSourcing.html)