From dcfc25d4ba426dc3d20b4cdf6ee5379186254722 Mon Sep 17 00:00:00 2001 From: Michal Sokolowski Date: Fri, 1 Nov 2019 16:32:45 +0100 Subject: [PATCH] Update /_example/basic/3-router example (#157) --- _examples/basic/3-router/go.mod | 2 +- _examples/basic/3-router/go.sum | 4 ++-- _examples/basic/3-router/main.go | 14 +++++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/_examples/basic/3-router/go.mod b/_examples/basic/3-router/go.mod index a1dc09b8c..f2c82c917 100644 --- a/_examples/basic/3-router/go.mod +++ b/_examples/basic/3-router/go.mod @@ -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 diff --git a/_examples/basic/3-router/go.sum b/_examples/basic/3-router/go.sum index 84108eb08..a76728e3c 100644 --- a/_examples/basic/3-router/go.sum +++ b/_examples/basic/3-router/go.sum @@ -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= diff --git a/_examples/basic/3-router/main.go b/_examples/basic/3-router/main.go index eb5afd6af..11efccd3e 100644 --- a/_examples/basic/3-router/main.go +++ b/_examples/basic/3-router/main.go @@ -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, @@ -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, @@ -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",