This commit is contained in:
parent
fd4ddaf6d6
commit
59668090df
|
@ -13,6 +13,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"p83.nl/go/websub-hub/cmd/hubserver/storage"
|
||||
)
|
||||
|
||||
|
@ -28,27 +29,31 @@ func (handler *subscriptionHandler) handlePublish(w http.ResponseWriter, r *http
|
|||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", topic, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrap(err, "could not create request")
|
||||
}
|
||||
req.Header.Add("Accept", "*/*")
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrapf(err, "could not request topic %s", topic)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
feedContentType := res.Header.Get("Content-Type")
|
||||
feedContent, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrap(err, "could not read body")
|
||||
}
|
||||
|
||||
subs, err := handler.store.Subscribers(topic)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not get subscribers")
|
||||
}
|
||||
|
||||
if subs, err := handler.store.Subscribers(topic); err != nil {
|
||||
for _, sub := range subs {
|
||||
log.Printf("publish: creating post to %s\n", sub.Callback)
|
||||
postReq, err := http.NewRequest("POST", sub.Callback, strings.NewReader(string(feedContent)))
|
||||
if err != nil {
|
||||
log.Printf("While creating request to %s: %s", sub.Callback, err)
|
||||
log.Printf("could not creating request to %s: %s", sub.Callback, err)
|
||||
continue
|
||||
}
|
||||
postReq.Header.Add("Content-Type", feedContentType)
|
||||
|
@ -66,14 +71,13 @@ func (handler *subscriptionHandler) handlePublish(w http.ResponseWriter, r *http
|
|||
}
|
||||
postRes, err := client.Do(postReq)
|
||||
if err != nil {
|
||||
log.Printf("While POSTing to %s: %s", sub.Callback, err)
|
||||
log.Printf("could not publish to %s: %s", sub.Callback, err)
|
||||
continue
|
||||
}
|
||||
log.Printf("publish: post send to %s\n", sub.Callback)
|
||||
log.Println("Response:")
|
||||
_ = postRes.Write(os.Stdout)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user