forked from ThreeDotsLabs/watermill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (90 loc) · 2.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"context"
"encoding/json"
"flag"
"io/ioutil"
stdHttp "net/http"
_ "net/http/pprof"
"github.com/pkg/errors"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill-http/pkg/http"
"github.com/ThreeDotsLabs/watermill-kafka/v2/pkg/kafka"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
"github.com/ThreeDotsLabs/watermill/message/router/plugin"
)
var (
kafkaAddr = flag.String("kafka", "localhost:9092", "The address of the kafka broker")
httpAddr = flag.String("http", ":8080", "The address for the http subscriber")
)
type Webhook struct {
ObjectKind string `json:"object_kind"`
}
func main() {
flag.Parse()
logger := watermill.NewStdLogger(true, true)
kafkaPublisher, err := kafka.NewPublisher(
kafka.PublisherConfig{
Brokers: []string{*kafkaAddr},
Marshaler: kafka.DefaultMarshaler{},
},
logger,
)
if err != nil {
panic(err)
}
httpSubscriber, err := http.NewSubscriber(
*httpAddr,
http.SubscriberConfig{
UnmarshalMessageFunc: func(topic string, request *stdHttp.Request) (*message.Message, error) {
b, err := ioutil.ReadAll(request.Body)
if err != nil {
return nil, errors.Wrap(err, "cannot read body")
}
return message.NewMessage(watermill.NewUUID(), b), nil
},
},
logger,
)
if err != nil {
panic(err)
}
r, err := message.NewRouter(
message.RouterConfig{},
logger,
)
if err != nil {
panic(err)
}
r.AddMiddleware(
middleware.Recoverer,
middleware.CorrelationID,
)
r.AddPlugin(plugin.SignalsHandler)
r.AddHandler(
"http_to_kafka",
"/webhooks", // this is the URL of our API
httpSubscriber,
"webhooks", // this is the topic the message will be published to
kafkaPublisher,
func(msg *message.Message) ([]*message.Message, error) {
webhook := Webhook{}
if err := json.Unmarshal(msg.Payload, &webhook); err != nil {
return nil, errors.Wrap(err, "cannot unmarshal message")
}
// Add simple validation
if webhook.ObjectKind == "" {
return nil, errors.New("empty object kind")
}
// Simply forward the message from HTTP Subscriber to Kafka Publisher
return []*message.Message{msg}, nil
},
)
go func() {
// HTTP server needs to be started after router is ready.
<-r.Running()
_ = httpSubscriber.StartHTTPServer()
}()
_ = r.Run(context.Background())
}