From 93bc38db67bb0c989001b37cb4c81cef5b05df98 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Tue, 5 Mar 2024 16:04:49 +0100 Subject: [PATCH] Try out different rss package --- cmd/data/articles.go | 2 +- cmd/data/rss.go | 106 ------------------------------------------- cmd/ui/articles.go | 17 ++++--- cmd/ui/rss.go | 7 ++- go.mod | 2 +- go.sum | 12 ++--- 6 files changed, 17 insertions(+), 129 deletions(-) delete mode 100644 cmd/data/rss.go diff --git a/cmd/data/articles.go b/cmd/data/articles.go index 4733ab8..858a38c 100644 --- a/cmd/data/articles.go +++ b/cmd/data/articles.go @@ -11,12 +11,12 @@ import ( ) type Article struct { + Tags *TagList Title string Author string Created time.Time Desc string Content string - Tags []string UUID uuid.UUID AuthorID int64 } diff --git a/cmd/data/rss.go b/cmd/data/rss.go deleted file mode 100644 index 3801f10..0000000 --- a/cmd/data/rss.go +++ /dev/null @@ -1,106 +0,0 @@ -package data - -import ( - "encoding/gob" - "fmt" - "os" - "sync" - - "github.com/gorilla/feeds" -) - -type Feed struct { - addCh chan *feeds.Item - setCh chan feeds.Feed - getCh chan feeds.Feed - feed feeds.Feed - wg sync.WaitGroup -} - -func minFeed() *Feed { - return &Feed{ - addCh: make(chan *feeds.Item), - setCh: make(chan feeds.Feed), - getCh: make(chan feeds.Feed), - } -} - -func (f *Feed) start() { - f.wg.Done() - for { - select { - case item := <-f.addCh: - f.feed.Items = append(f.feed.Items, item) - case f.getCh <- f.feed: - case f.feed = <-f.setCh: - } - } -} - -func NewFeed(title, link, desc string) *Feed { - feed := minFeed() - feed.feed = feeds.Feed{ - Title: title, - Link: &feeds.Link{Href: link}, - Description: desc, - } - - feed.wg.Add(1) - go feed.start() - feed.wg.Wait() - - return feed -} - -func (f *Feed) Get() feeds.Feed { - return <-f.getCh -} - -func (f *Feed) Set(feed feeds.Feed) { - f.setCh <- feed -} - -func OpenFeed(filename string) (*Feed, error) { - file, err := os.Open(filename) - if err != nil { - return nil, fmt.Errorf("error opening file %v: %v", filename, err) - } - defer file.Close() - - feed := minFeed() - feed.wg.Add(1) - go feed.start() - feed.wg.Wait() - - decoder := gob.NewDecoder(file) - tmpFeed := new(feeds.Feed) - err = decoder.Decode(tmpFeed) - if err != nil { - return nil, fmt.Errorf("error decoding file %v: %v", filename, err) - } - - feed.Set(*tmpFeed) - - return feed, nil -} - -func (f *Feed) Save(filename string) error { - file, err := os.Create(filename) - if err != nil { - return fmt.Errorf("error creating file %v: %v", filename, err) - } - defer file.Close() - - encoder := gob.NewEncoder(file) - feed := f.Get() - err = encoder.Encode(feed) - if err != nil { - return fmt.Errorf("error encoding file %v: %v", filename, err) - } - - return nil -} - -func (f *Feed) Add(i *feeds.Item) { - f.addCh <- i -} diff --git a/cmd/ui/articles.go b/cmd/ui/articles.go index 6d3823a..3ec3e5c 100644 --- a/cmd/ui/articles.go +++ b/cmd/ui/articles.go @@ -6,8 +6,8 @@ import ( "net/http" "time" + "git.streifling.com/jason/rss" "github.com/google/uuid" - "github.com/gorilla/feeds" "streifling.com/jason/cpolis/cmd/data" ) @@ -116,7 +116,7 @@ func ReviewArticle(al *data.ArticleList, s *data.CookieStore) http.HandlerFunc { } } -func PublishArticle(f *data.Feed, al *data.ArticleList, s *data.CookieStore) http.HandlerFunc { +func PublishArticle(f *rss.Feed, al *data.ArticleList, s *data.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { uuid, err := uuid.Parse(r.PostFormValue("uuid")) if err != nil { @@ -141,13 +141,12 @@ func PublishArticle(f *data.Feed, al *data.ArticleList, s *data.CookieStore) htt template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) } - f.Add(&feeds.Item{ - Title: article.Title, - Author: &feeds.Author{Name: session.Values["name"].(string)}, - Created: article.Created, - Description: article.Desc, - Content: article.Content, - }) + item := rss.NewItem() + item.Title = article.Title + item.Author = article.Author + item.Description = article.Desc + item.Content = &rss.Content{Value: article.Content} + f.Channels[0].AddItem(item) f.Save("tmp/rss.gob") tmpl, err := template.ParseFiles("web/templates/hub.html") diff --git a/cmd/ui/rss.go b/cmd/ui/rss.go index 267bef9..e856cda 100644 --- a/cmd/ui/rss.go +++ b/cmd/ui/rss.go @@ -5,13 +5,12 @@ import ( "log" "net/http" - "streifling.com/jason/cpolis/cmd/data" + "git.streifling.com/jason/rss" ) -func ShowRSS(f *data.Feed) http.HandlerFunc { +func ShowRSS(f *rss.Feed) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - feed := f.Get() - rss, err := feed.ToRss() + rss, err := f.ToXML() if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/go.mod b/go.mod index 69472b2..9e88be2 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module streifling.com/jason/cpolis go 1.22.0 require ( + git.streifling.com/jason/rss v0.0.0-20240305145359-7d49b2cb25fc github.com/go-sql-driver/mysql v1.7.1 github.com/google/uuid v1.6.0 - github.com/gorilla/feeds v1.1.2 github.com/gorilla/sessions v1.2.2 github.com/microcosm-cc/bluemonday v1.0.26 github.com/yuin/goldmark v1.7.0 diff --git a/go.sum b/go.sum index 6c5daec..726c26e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +git.streifling.com/jason/rss v0.0.0-20240305140009-a59d3f112892 h1:miU3U9H8zSoYprEaG7xaGLMb4CcGLjGt7McC8Wrf+Vs= +git.streifling.com/jason/rss v0.0.0-20240305140009-a59d3f112892/go.mod h1:gpZF0nZbQSstMpyHD9DTAvlQEG7v4pjO5c7aIMWM4Jg= +git.streifling.com/jason/rss v0.0.0-20240305145359-7d49b2cb25fc h1:vJ36ouI2wTK+jFktnqyAfFHoYnoznAgAo1nUzvMzCvQ= +git.streifling.com/jason/rss v0.0.0-20240305145359-7d49b2cb25fc/go.mod h1:gpZF0nZbQSstMpyHD9DTAvlQEG7v4pjO5c7aIMWM4Jg= github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= @@ -8,20 +12,12 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= -github.com/gorilla/feeds v1.1.2 h1:pxzZ5PD3RJdhFH2FsJJ4x6PqMqbgFk1+Vez4XWBW8Iw= -github.com/gorilla/feeds v1.1.2/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y= github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY= github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ= -github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58= github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs= -github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= -github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA= github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=