-
Notifications
You must be signed in to change notification settings - Fork 17
/
example.go
78 lines (61 loc) · 1.86 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2014 Daniel Pupius
// A very simple PubSubHubbub subscriber client that watches a feed that can be
// easily updated by visiting http://push-pub.appspot.com/.
//
// Usage:
// $ go run example/example.go --host=[host or ip] --port=[port number]
//
// The Hub will need to be able to communicate with your server. If your dev
// machine isn't publically accessible consider setting up reverse port
// forwarding from EC2 or a VPS (see https://medium.com/dev-tricks/220030f3c84a)
//
// To gracefully shutdown the server and unsubscribe from the hub, press enter.
//
package main
import (
"encoding/xml"
"flag"
"fmt"
"log"
"time"
"github.com/dpup/gohubbub"
)
type Feed struct {
Status string `xml:"status>http"`
Entries []Entry `xml:"entry"`
}
type Entry struct {
URL string `xml:"id"`
Published string `xml:"published"`
Title string `xml:"title"`
Content string `xml:"content"`
}
var host = flag.String("host", "", "Host or IP to serve from")
var port = flag.Int("port", 10000, "The port to serve from")
func main() {
flag.Parse()
log.Println("PubSubHubbub Subscriber Started")
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)
if xmlError != nil {
log.Printf("XML Parse Error %v", xmlError)
} else {
log.Println("Feed status:", feed.Status)
for _, entry := range feed.Entries {
log.Printf("%s - %s (%s)", entry.Title, entry.Content, entry.URL)
}
}
})
if err != nil {
log.Fatal(err)
}
go client.StartAndServe("", *port)
time.Sleep(time.Second * 5)
log.Println("Press Enter for graceful shutdown...")
var input string
fmt.Scanln(&input)
client.Unsubscribe("http://push-pub.appspot.com/feed")
time.Sleep(time.Second * 5)
}