Skip to content

Commit

Permalink
Differentiate "self" from the address and port the server listens on
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Pupius committed May 17, 2014
1 parent 9413159 commit 2dc6969
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import "github.com/dpup/gohubbub"

// ...

client := gohubbub.NewClient(hostname, port, "Testing")
client := gohubbub.NewClient("mysite.com", "Testing")
client.DiscoverAndSubscribe(topicURL, func(contentType string, body []byte) {
// Handle update notification.
})
client.StartAndServe()
client.StartAndServe("", 80)
```

Or if you have your own server, you can register the gohubbub request handler on
Expand All @@ -31,7 +31,7 @@ import "github.com/dpup/gohubbub"

// ...

client := gohubbub.NewClient(hostname, port, "Testing")
client := gohubbub.NewClient("mysite.com", "Testing")
client.DiscoverAndSubscribe(topicURL, func(contentType string, body []byte) {
// Handle update notification.
})
Expand Down
4 changes: 2 additions & 2 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {

log.Println("PubSubHubbub Subscriber Started")

client := gohubbub.NewClient(*host, *port, "Test App")
client := gohubbub.NewClient(fmt.Sprintf("%s:%d", *host, *port), "Test App")
err := client.DiscoverAndSubscribe("http://push-pub.appspot.com/feed", func(contentType string, body []byte) {
var feed Feed
xmlError := xml.Unmarshal(body, &feed)
Expand All @@ -64,7 +64,7 @@ func main() {
log.Fatal(err)
}

go client.StartAndServe()
go client.StartAndServe("", *port)

time.Sleep(time.Second * 5)
log.Println("Press Enter for graceful shutdown...")
Expand Down
4 changes: 2 additions & 2 deletions example/medium.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {

log.Println("Medium Story Watcher Started...")

client := gohubbub.NewClient(*host, *port, "MediumStoryWatcher")
client := gohubbub.NewClient(fmt.Sprintf("%s:%d", *host, *port), "MediumStoryWatcher")
err := client.DiscoverAndSubscribe("https://medium.com/feed/latest", func(contentType string, body []byte) {
var feed Feed
xmlError := xml.Unmarshal(body, &feed)
Expand All @@ -60,7 +60,7 @@ func main() {
log.Fatal(err)
}

go client.StartAndServe()
go client.StartAndServe("", *port)

time.Sleep(time.Second * 5)
log.Println("Press Enter for graceful shutdown...")
Expand Down
16 changes: 7 additions & 9 deletions gohubbub.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,20 @@ type HttpRequester interface {
// Client allows you to register a callback for PubSubHubbub subscriptions,
// handlers will be executed when an update is received.
type Client struct {
// Hostname or IP address that the client will be served from, should be
// accessible by the hub. e.g. "push.myhost.com"
// Hostname or IP address and port that remote client will connect to, should
// be accessible by the hub. e.g. "push.myhost.com:4100"
self string

port int // Which port the server will be started on.
from string // String passed in the "From" header.
running bool // Whether the server is running.
subscriptions map[string]*subscription // Map of subscriptions.
httpRequester HttpRequester // e.g. http.Client{}.
history *ring.Ring // Stores past messages, for deduplication.
}

func NewClient(self string, port int, from string) *Client {
func NewClient(self string, from string) *Client {
return &Client{
self,
port,
fmt.Sprintf("%s (gohubbub)", from),
false,
make(map[string]*subscription),
Expand Down Expand Up @@ -152,7 +150,7 @@ func (client *Client) Unsubscribe(topic string) {

// StartAndServe starts a server using DefaultServeMux, and makes initial
// subscription requests.
func (client *Client) StartAndServe() {
func (client *Client) StartAndServe(addr string, port int) {
client.RegisterHandler(http.DefaultServeMux)

// For default server give other paths a noop endpoint.
Expand All @@ -161,8 +159,8 @@ func (client *Client) StartAndServe() {
// Trigger subscription requests async.
go client.Start()

log.Printf("Starting HTTP server on port %d", client.port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", client.port), nil))
log.Printf("Starting HTTP server on %s:%d", addr, port)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", addr, port), nil))
}

// RegisterHandler binds the client's HandlerFunc to the provided MUX on the
Expand Down Expand Up @@ -253,7 +251,7 @@ func (client *Client) makeUnsubscribeRequeast(s *subscription) {
}

func (client *Client) formatCallbackURL(callback int) string {
return fmt.Sprintf("http://%s:%d/push-callback/%d", client.self, client.port, callback)
return fmt.Sprintf("http://%s/push-callback/%d", client.self, callback)
}

func (client *Client) handleDefaultRequest(resp http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit 2dc6969

Please sign in to comment.