feat: add path

This commit is contained in:
Kevin Franklin Kim 2024-03-12 13:42:04 +01:00
parent 391f82e2f0
commit 5125125257
No known key found for this signature in database

View File

@ -22,7 +22,8 @@ var (
type (
Publisher struct {
l *zap.Logger
url string
host string
path string
client *http.Client
marshalMessageFunc PublisherMarshalMessageFunc
closed bool
@ -36,10 +37,11 @@ type (
// ~ Constructor
// ------------------------------------------------------------------------------------------------
func NewPublisher(l *zap.Logger, url string, opts ...PublisherOption) *Publisher {
func NewPublisher(l *zap.Logger, host string, opts ...PublisherOption) *Publisher {
inst := &Publisher{
l: l,
url: url,
host: host,
path: "/g/collect",
client: http.DefaultClient,
}
for _, opt := range opts {
@ -52,6 +54,12 @@ func NewPublisher(l *zap.Logger, url string, opts ...PublisherOption) *Publisher
// ~ Options
// ------------------------------------------------------------------------------------------------
func PublisherWithPath(v string) PublisherOption {
return func(o *Publisher) {
o.path = v
}
}
func PublisherWithClient(v *http.Client) PublisherOption {
return func(o *Publisher) {
o.client = v
@ -92,7 +100,7 @@ func (p *Publisher) Publish(topic string, messages ...*message.Message) error {
return err
}
req, err := http.NewRequestWithContext(msg.Context(), http.MethodPost, fmt.Sprintf("%s?%s", p.url, mpv2.EncodeValues(values)), body)
req, err := http.NewRequestWithContext(msg.Context(), http.MethodPost, fmt.Sprintf("%s%s?%s", p.host, p.path, mpv2.EncodeValues(values)), body)
if err != nil {
return errors.Wrap(err, "failed to create request")
}