-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kamil Mosciszko <[email protected]>
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
gbus "github.com/Raezil/GoEventBus" | ||
|
||
_ "github.com/lib/pq" | ||
) | ||
|
||
// HouseWasSold represents an event for when a house has been sold | ||
type HouseWasSold struct{} | ||
|
||
// NewDispatcher initializes the dispatcher with event handlers | ||
func NewDispatcher() *gbus.Dispatcher { | ||
return &gbus.Dispatcher{ | ||
HouseWasSold{}: func(m map[string]interface{}) (gbus.Result, error) { | ||
price, ok := m["price"].(int) | ||
if !ok { | ||
return gbus.Result{}, fmt.Errorf("price not provided or invalid") | ||
} | ||
result := fmt.Sprintf("House was sold for %d", price) | ||
log.Println(result) | ||
return gbus.Result{ | ||
Message: result, | ||
}, nil | ||
}, | ||
} | ||
} | ||
|
||
func main() { | ||
// Initialize dispatcher and event store | ||
dispatcher := NewDispatcher() | ||
eventstore := gbus.NewEventStore(dispatcher) | ||
eventstore.Publish(gbus.NewEvent( | ||
HouseWasSold{}, | ||
map[string]interface{}{ | ||
"price": 100, | ||
}, | ||
)) | ||
|
||
eventstore.Publish(gbus.NewEvent( | ||
HouseWasSold{}, | ||
map[string]interface{}{ | ||
"price": 100, | ||
}, | ||
)) | ||
// Broadcast the event | ||
eventstore.Broadcast() | ||
|
||
} |