Skip to content

Commit

Permalink
Update /_example/basic/3-router example (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Sokolowski authored and roblaszczak committed Nov 1, 2019
1 parent c72e26a commit dcfc25d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion _examples/basic/3-router/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module main.go

require github.com/ThreeDotsLabs/watermill v1.0.0-rc.2
require github.com/ThreeDotsLabs/watermill v1.0.1-0.20191022063524-9ce413590da0
4 changes: 2 additions & 2 deletions _examples/basic/3-router/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/ThreeDotsLabs/watermill v1.0.0-rc.2 h1:DCKR8kSLHUyZny6LiKtO7h+IZPMo0t9x786FOZmoAh4=
github.com/ThreeDotsLabs/watermill v1.0.0-rc.2/go.mod h1:gjVFKc8aN+vmEHw3pA0kh4mmuwHe02nyfghb6IWWiKE=
github.com/ThreeDotsLabs/watermill v1.0.1-0.20191022063524-9ce413590da0 h1:3Pf5VPjh+tigAZLhZgyZ4vHm23dZmVYAsy26qs8Zu28=
github.com/ThreeDotsLabs/watermill v1.0.1-0.20191022063524-9ce413590da0/go.mod h1:gjVFKc8aN+vmEHw3pA0kh4mmuwHe02nyfghb6IWWiKE=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down
14 changes: 13 additions & 1 deletion _examples/basic/3-router/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func main() {
// You can also close the router by just calling `r.Close()`.
router.AddPlugin(plugin.SignalsHandler)

// Router level middleware are executed for every message sent to the router
router.AddMiddleware(
// CorrelationID will copy the correlation id from the incoming message's metadata to the produced messages
middleware.CorrelationID,
Expand All @@ -54,7 +55,8 @@ func main() {
// Producing some incoming messages in background
go publishMessages(pubSub)

router.AddHandler(
// AddHandler returns a handler which can be used to add handler level middleware
handler := router.AddHandler(
"struct_handler", // handler name, must be unique
"incoming_messages_topic", // topic from which we will read events
pubSub,
Expand All @@ -63,6 +65,16 @@ func main() {
structHandler{}.Handler,
)

// Handler level middleware is only executed for a specific handler
// Such middleware can be added the same way the router level ones
handler.AddMiddleware(func(h message.HandlerFunc) message.HandlerFunc {
return func(message *message.Message) ([]*message.Message, error) {
log.Println("executing handler specific middleware for ", message.UUID)

return h(message)
}
})

// just for debug, we are printing all messages received on `incoming_messages_topic`
router.AddNoPublisherHandler(
"print_incoming_messages",
Expand Down

0 comments on commit dcfc25d

Please sign in to comment.