From e8796eae5a276b2615ebf6dad014fc222f097de0 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sat, 7 Jul 2018 17:59:41 +0200 Subject: [PATCH] Extract hubbackend.go --- cmd/eksterd/hubbackend.go | 113 ++++++++++++++++++++++++++++++++++++++ cmd/eksterd/incoming.go | 2 +- cmd/eksterd/main.go | 102 ---------------------------------- 3 files changed, 114 insertions(+), 103 deletions(-) create mode 100644 cmd/eksterd/hubbackend.go diff --git a/cmd/eksterd/hubbackend.go b/cmd/eksterd/hubbackend.go new file mode 100644 index 0000000..e532955 --- /dev/null +++ b/cmd/eksterd/hubbackend.go @@ -0,0 +1,113 @@ +package main + +import ( + "fmt" + "io" + "log" + "net/http" + "os" + "time" + + "github.com/garyburd/redigo/redis" + "github.com/pstuifzand/ekster/pkg/util" + "github.com/pstuifzand/ekster/pkg/websub" +) + +type hubIncomingBackend struct { + backend *memoryBackend +} + +func (h *hubIncomingBackend) GetSecret(id int64) string { + conn := pool.Get() + defer conn.Close() + secret, err := redis.String(conn.Do("HGET", fmt.Sprintf("feed:%d", id), "secret")) + if err != nil { + return "" + } + return secret +} + +func (h *hubIncomingBackend) CreateFeed(topic string, channel string) (int64, error) { + conn := pool.Get() + defer conn.Close() + id, err := redis.Int64(conn.Do("INCR", "feed:next_id")) + + if err != nil { + return 0, err + } + + conn.Do("HSET", fmt.Sprintf("feed:%d", id), "url", topic) + conn.Do("HSET", fmt.Sprintf("feed:%d", id), "channel", channel) + secret := util.RandStringBytes(16) + conn.Do("HSET", fmt.Sprintf("feed:%d", id), "secret", secret) + + client := &http.Client{} + + hubURL, err := websub.GetHubURL(client, topic) + if hubURL == "" { + log.Printf("WebSub Hub URL not found for topic=%s\n", topic) + } else { + log.Printf("WebSub Hub URL found for topic=%s hub=%s\n", topic, hubURL) + } + + callbackURL := fmt.Sprintf("%s/incoming/%d", os.Getenv("EKSTER_BASEURL"), id) + + if err == nil && hubURL != "" { + args := redis.Args{}.Add(fmt.Sprintf("feed:%d", id), "hub", hubURL, "callback", callbackURL) + conn.Do("HMSET", args...) + } else { + return id, nil + } + + websub.Subscribe(client, hubURL, topic, callbackURL, secret, 24*3600) + + return id, nil +} + +func (h *hubIncomingBackend) UpdateFeed(feedID int64, contentType string, body io.Reader) error { + conn := pool.Get() + defer conn.Close() + log.Printf("updating feed %d", feedID) + u, err := redis.String(conn.Do("HGET", fmt.Sprintf("feed:%d", feedID), "url")) + if err != nil { + return err + } + channel, err := redis.String(conn.Do("HGET", fmt.Sprintf("feed:%d", feedID), "channel")) + if err != nil { + return err + } + + log.Printf("updating feed %d - %s %s\n", feedID, u, channel) + h.backend.ProcessContent(channel, u, contentType, body) + + return err +} + +func (h *hubIncomingBackend) FeedSetLeaseSeconds(feedID int64, leaseSeconds int64) error { + conn := pool.Get() + defer conn.Close() + log.Printf("updating feed %d lease_seconds", feedID) + + args := redis.Args{}.Add(fmt.Sprintf("feed:%d", feedID), "lease_seconds", leaseSeconds) + conn.Do("HSET", args...) + + return nil +} + +func (h *hubIncomingBackend) run() error { + ticker := time.NewTicker(10 * time.Minute) + quit := make(chan struct{}) + + go func() { + for { + select { + case <-ticker.C: + case <-quit: + ticker.Stop() + return + } + } + }() + + return nil +} diff --git a/cmd/eksterd/incoming.go b/cmd/eksterd/incoming.go index ada80f0..056b29b 100644 --- a/cmd/eksterd/incoming.go +++ b/cmd/eksterd/incoming.go @@ -17,7 +17,7 @@ import ( // HubBackend handles information for the incoming handler type HubBackend interface { CreateFeed(url, channel string) (int64, error) - GetSecret(id int64) string + GetSecret(feedID int64) string UpdateFeed(feedID int64, contentType string, body io.Reader) error FeedSetLeaseSeconds(feedID int64, leaseSeconds int64) error } diff --git a/cmd/eksterd/main.go b/cmd/eksterd/main.go index f8a6cd4..e65b54f 100644 --- a/cmd/eksterd/main.go +++ b/cmd/eksterd/main.go @@ -20,7 +20,6 @@ package main import ( "flag" "fmt" - "io" "log" "net/http" "os" @@ -29,8 +28,6 @@ import ( "github.com/garyburd/redigo/redis" "github.com/pstuifzand/ekster/pkg/microsub" - "github.com/pstuifzand/ekster/pkg/util" - "github.com/pstuifzand/ekster/pkg/websub" ) var ( @@ -61,105 +58,6 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) } -type hubIncomingBackend struct { - backend *memoryBackend -} - -func (h *hubIncomingBackend) GetSecret(id int64) string { - conn := pool.Get() - defer conn.Close() - secret, err := redis.String(conn.Do("HGET", fmt.Sprintf("feed:%d", id), "secret")) - if err != nil { - return "" - } - return secret -} - -func (h *hubIncomingBackend) CreateFeed(topic string, channel string) (int64, error) { - conn := pool.Get() - defer conn.Close() - id, err := redis.Int64(conn.Do("INCR", "feed:next_id")) - - if err != nil { - return 0, err - } - - conn.Do("HSET", fmt.Sprintf("feed:%d", id), "url", topic) - conn.Do("HSET", fmt.Sprintf("feed:%d", id), "channel", channel) - secret := util.RandStringBytes(16) - conn.Do("HSET", fmt.Sprintf("feed:%d", id), "secret", secret) - - client := &http.Client{} - - hubURL, err := websub.GetHubURL(client, topic) - if hubURL == "" { - log.Printf("WebSub Hub URL not found for topic=%s\n", topic) - } else { - log.Printf("WebSub Hub URL found for topic=%s hub=%s\n", topic, hubURL) - } - - callbackURL := fmt.Sprintf("%s/incoming/%d", os.Getenv("EKSTER_BASEURL"), id) - - if err == nil && hubURL != "" { - args := redis.Args{}.Add(fmt.Sprintf("feed:%d", id), "hub", hubURL, "callback", callbackURL) - conn.Do("HMSET", args...) - } else { - return id, nil - } - - websub.Subscribe(client, hubURL, topic, callbackURL, secret, 24*3600) - - return id, nil -} - -func (h *hubIncomingBackend) UpdateFeed(feedID int64, contentType string, body io.Reader) error { - conn := pool.Get() - defer conn.Close() - log.Printf("updating feed %d", feedID) - u, err := redis.String(conn.Do("HGET", fmt.Sprintf("feed:%d", feedID), "url")) - if err != nil { - return err - } - channel, err := redis.String(conn.Do("HGET", fmt.Sprintf("feed:%d", feedID), "channel")) - if err != nil { - return err - } - - log.Printf("updating feed %d - %s %s\n", feedID, u, channel) - h.backend.ProcessContent(channel, u, contentType, body) - - return err -} - -func (h *hubIncomingBackend) FeedSetLeaseSeconds(feedID int64, leaseSeconds int64) error { - conn := pool.Get() - defer conn.Close() - log.Printf("updating feed %d lease_seconds", feedID) - - args := redis.Args{}.Add(fmt.Sprintf("feed:%d", feedID), "lease_seconds", leaseSeconds) - conn.Do("HSET", args...) - - return nil -} - -func (h *hubIncomingBackend) run() error { - ticker := time.NewTicker(10 * time.Minute) - quit := make(chan struct{}) - - go func() { - for { - select { - case <-ticker.C: - case <-quit: - ticker.Stop() - return - } - } - }() - - return nil -} - func newPool(addr string) *redis.Pool { return &redis.Pool{ MaxIdle: 3,