github.com/wfusion/gofusion@v1.1.14/common/infra/watermill/docs/content/pubsubs/http.md (about) 1 +++ 2 title = "HTTP" 3 description = "Call and listen to webhooks asynchronously" 4 date = 2019-07-06T22:30:00+02:00 5 bref = "Call and listen to webhooks asynchronously" 6 weight = -70 7 type = "docs" 8 toc = false 9 +++ 10 11 ### HTTP 12 13 The HTTP subscriber listens to HTTP requests (for example - webhooks) and outputs them as messages. 14 You can then post them to any Publisher. Here is an example with [sending HTTP messages to Kafka](https://github.com/ThreeDotsLabs/watermill/blob/master/_examples/real-world-examples/receiving-webhooks/main.go). 15 16 The HTTP publisher sends HTTP requests as specified in its configuration. Here is an example with [transforming Kafka messages into HTTP webhook requests](https://github.com/ThreeDotsLabs/watermill/tree/master/_examples/real-world-examples/sending-webhooks). 17 18 ### Installation 19 20 go get github.com/ThreeDotsLabs/watermill-http 21 22 #### Characteristics 23 24 | Feature | Implements | Note | 25 | ------- | ---------- | ---- | 26 | ConsumerGroups | no | | 27 | ExactlyOnceDelivery | yes | | 28 | GuaranteedOrder | yes | | 29 | Persistent | no| | 30 31 #### Subscriber configuration 32 33 Subscriber configuration is done via the config struct passed to the constructor: 34 35 {{% render-md %}} 36 {{% load-snippet-partial file="src-link/watermill-http/pkg/http/subscriber.go" first_line_contains="type SubscriberConfig struct" last_line_contains="}" %}} 37 {{% /render-md %}} 38 39 You can use the `Router` config option to `SubscriberConfig` to pass your own `chi.Router` (see [chi](https://github.com/go-chi/chi)). 40 This may be helpful if you'd like to add your own HTTP handlers (e.g. a health check endpoint). 41 42 #### Publisher configuration 43 44 Publisher configuration is done via the config struct passed to the constructor: 45 46 {{% render-md %}} 47 {{% load-snippet-partial file="src-link/watermill-http/pkg/http/publisher.go" first_line_contains="type PublisherConfig struct" last_line_contains="}" %}} 48 {{% /render-md %}} 49 50 How the message topic and body translate into the URL, method, headers, and payload of the HTTP request is highly configurable through the use of `MarshalMessageFunc`. 51 Use the provided `DefaultMarshalMessageFunc` to send POST requests to a specific url: 52 53 {{% render-md %}} 54 {{% load-snippet-partial file="src-link/watermill-http/pkg/http/publisher.go" first_line_contains="// MarshalMessageFunc" last_line_contains="return req, nil" padding_after="2" %}} 55 {{% /render-md %}} 56 57 You can pass your own `http.Client` to execute the requests or use Golang's default client. 58 59 #### Running 60 61 To run HTTP subscriber you need to run `StartHTTPServer()`. It needs to be run after `Subscribe()`. 62 63 When using with the router, you should wait for the router to start. 64 65 {{< highlight >}} 66 <-r.Running() 67 httpSubscriber.StartHTTPServer() 68 {{< /highlight >}} 69 70 #### Subscribing 71 72 {{% render-md %}} 73 {{% load-snippet-partial file="src-link/watermill-http/pkg/http/subscriber.go" first_line_contains="// Subscribe adds" last_line_contains="func (s *Subscriber) Subscribe" %}} 74 {{% /render-md %}} 75