func NewPublisher(config PublisherConfig, logger watermill.LoggerAdapter) (*Publisher, error) {
	config.setDefaults()
	if err := config.validate(); err != nil {
		return nil, errors.Wrap(err, "invalid Publisher config")
	}
	if logger == nil {
		logger = watermill.NopLogger{}
	}
	return &Publisher{
		config: config,
		logger: logger,
	}, nil
}
NewPublisher creates a new Publisher.
It publishes the received messages as HTTP requests.
The URL, method and payload of the request are determined by the configured MarshalMessageFunc.
package http
import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"github.com/pkg/errors"
	"github.com/ThreeDotsLabs/watermill"
	"github.com/ThreeDotsLabs/watermill/message"
)
var (
	ErrPublisherClosed = errors.New("publisher is closed")
	ErrNoMarshalFunc   = errors.New("marshal function is missing")
	ErrErrorResponse   = errors.New("server responded with error status")
)
type MarshalMessageFunc func(url string, msg *message.Message) (*http.Request, error)
func DefaultMarshalMessageFunc(url string, msg *message.Message) (*http.Request, error) {
	req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(msg.Payload))
	if err != nil {
		return nil, err
	}
	req.Header.Set(HeaderUUID, msg.UUID)
	metadataJson, err := json.Marshal(msg.Metadata)
	if err != nil {
		return nil, errors.Wrap(err, "could not marshal metadata to JSON")
	}
	req.Header.Set(HeaderMetadata, string(metadataJson))
	return req, nil
}
type PublisherConfig struct {
	MarshalMessageFunc MarshalMessageFunc
	Client             *http.Client
	DoNotLogResponseBodyOnServerError bool
}
func (c *PublisherConfig) setDefaults() {
	if c.Client == nil {
		c.Client = http.DefaultClient
	}
}
func (c PublisherConfig) validate() error {
	if c.MarshalMessageFunc == nil {
		return ErrNoMarshalFunc
	}
	return nil
}
type Publisher struct {
	logger watermill.LoggerAdapter
	config PublisherConfig
	closed bool
}