Skip to content

Commit

Permalink
feat: add new http publisher (#457)
Browse files Browse the repository at this point in the history
* feat: add new http publisher

Create a new publisher endpoint, capable of publishing content
to an http feed.

Closes: #386

Signed-off-by: Yolanda Robla <[email protected]>
Signed-off-by: Yolanda Robla <[email protected]>

* fix: do not use additional config struct

Signed-off-by: Yolanda Robla <[email protected]>

---------

Signed-off-by: Yolanda Robla <[email protected]>
Signed-off-by: Yolanda Robla <[email protected]>
  • Loading branch information
yrobla authored Apr 19, 2024
1 parent 27cf1da commit 931ad58
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/config/scheduledfeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ossf/package-feeds/pkg/feeds/rubygems"
"github.com/ossf/package-feeds/pkg/publisher"
"github.com/ossf/package-feeds/pkg/publisher/gcppubsub"
"github.com/ossf/package-feeds/pkg/publisher/httpclientpubsub"
"github.com/ossf/package-feeds/pkg/publisher/kafkapubsub"
"github.com/ossf/package-feeds/pkg/publisher/stdout"
)
Expand Down Expand Up @@ -153,6 +154,14 @@ func (pc PublisherConfig) ToPublisher(ctx context.Context) (publisher.Publisher,
return nil, fmt.Errorf("failed to decode kafkapubsub config: %w", err)
}
return kafkapubsub.FromConfig(ctx, kafkaConfig)
case httpclientpubsub.PublisherType:
var httpClientConfig httpclientpubsub.Config
err = strictDecode(pc.Config, &httpClientConfig)
if err != nil {
return nil, fmt.Errorf("failed to decode httpclient config: %w", err)
}
return httpclientpubsub.FromConfig(ctx, httpClientConfig)

case stdout.PublisherType:
return stdout.New(), nil
default:
Expand Down
8 changes: 8 additions & 0 deletions pkg/publisher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ publisher:
topic: packagefeeds
```

### HTTP client

```
publisher:
type: http-client
config:
url: "http://target-server:8000/package_feeds_hook"
```
60 changes: 60 additions & 0 deletions pkg/publisher/httpclientpubsub/httpclientpubsub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package httpclientpubsub

import (
"bytes"
"context"
"errors"
"fmt"
"net/http"

log "github.com/sirupsen/logrus"
)

const PublisherType = "http-client"

var ErrHTTPRequestFailed = errors.New("HTTP request failed")

type Config struct {
URL string `mapstructure:"url"`
}

type HTTPClientPubSub struct {
url string
}

func New(ctx context.Context, url string) (*HTTPClientPubSub, error) {
pub := &HTTPClientPubSub{url: url}
return pub, nil
}

func (pub *HTTPClientPubSub) Name() string {
return PublisherType
}

func FromConfig(ctx context.Context, config Config) (*HTTPClientPubSub, error) {
return New(ctx, config.URL)
}

func (pub *HTTPClientPubSub) Send(ctx context.Context, body []byte) error {
log.Info("Sending event to HTTP client publisher")
// Print the url to the log so that we can see where the event is being sent.
req, err := http.NewRequest("POST", pub.url, bytes.NewReader(body))
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%w with status code: %d", ErrHTTPRequestFailed, resp.StatusCode)
}

return nil
}

0 comments on commit 931ad58

Please sign in to comment.