Converted RSS package to git.streifling.com/jason/rss

This commit is contained in:
Jason Streifling 2024-03-05 17:13:59 +01:00
parent dd50c4f385
commit a9c61c5a11
6 changed files with 67 additions and 57 deletions

View File

@ -6,96 +6,96 @@ import (
"os" "os"
"sync" "sync"
"github.com/gorilla/feeds" "git.streifling.com/jason/rss"
) )
type Feed struct { type Channel struct {
addCh chan *feeds.Item addCh chan *rss.Item
setCh chan feeds.Feed setCh chan rss.Channel
getCh chan feeds.Feed getCh chan rss.Channel
feed feeds.Feed channel rss.Channel
wg sync.WaitGroup wg sync.WaitGroup
} }
func initFeed() *Feed { func initChannel() *Channel {
return &Feed{ return &Channel{
addCh: make(chan *feeds.Item), addCh: make(chan *rss.Item),
setCh: make(chan feeds.Feed), setCh: make(chan rss.Channel),
getCh: make(chan feeds.Feed), getCh: make(chan rss.Channel),
} }
} }
func (f *Feed) start() { func (c *Channel) start() {
f.wg.Done() c.wg.Done()
for { for {
select { select {
case item := <-f.addCh: case item := <-c.addCh:
f.feed.Items = append(f.feed.Items, item) c.channel.Items = append(c.channel.Items, item)
case f.getCh <- f.feed: case c.getCh <- c.channel:
case f.feed = <-f.setCh: case c.channel = <-c.setCh:
} }
} }
} }
func NewFeed(title, link, desc string) *Feed { func NewChannel(title, link, desc string) *Channel {
feed := initFeed() channel := initChannel()
feed.feed = feeds.Feed{ channel.channel = rss.Channel{
Title: title, Title: title,
Link: &feeds.Link{Href: link}, Link: link,
Description: desc, Description: desc,
} }
feed.wg.Add(1) channel.wg.Add(1)
go feed.start() go channel.start()
feed.wg.Wait() channel.wg.Wait()
return feed return channel
} }
func (f *Feed) Get() feeds.Feed { func (c *Channel) Get() rss.Channel {
return <-f.getCh return <-c.getCh
} }
func (f *Feed) Set(feed feeds.Feed) { func (f *Channel) Set(channel rss.Channel) {
f.setCh <- feed f.setCh <- channel
} }
func OpenFeed(filename string) (*Feed, error) { func LoadChannel(filename string) (*Channel, error) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
return nil, fmt.Errorf("error opening file %v: %v", filename, err) return nil, fmt.Errorf("error opening file %v: %v", filename, err)
} }
defer file.Close() defer file.Close()
feed := initFeed() channel := initChannel()
feed.wg.Add(1) channel.wg.Add(1)
go feed.start() go channel.start()
feed.wg.Wait() channel.wg.Wait()
tmpFeed := new(feeds.Feed) tmpChannel := new(rss.Channel)
if err = gob.NewDecoder(file).Decode(tmpFeed); err != nil { if err = gob.NewDecoder(file).Decode(tmpChannel); err != nil {
return nil, fmt.Errorf("error decoding file %v: %v", filename, err) return nil, fmt.Errorf("error decoding channel from file %v: %v", filename, err)
} }
feed.Set(*tmpFeed) channel.Set(*tmpChannel)
return feed, nil return channel, nil
} }
func (f *Feed) Save(filename string) error { func (c *Channel) Save(filename string) error {
file, err := os.Create(filename) file, err := os.Create(filename)
if err != nil { if err != nil {
return fmt.Errorf("error creating file %v: %v", filename, err) return fmt.Errorf("error creating file %v: %v", filename, err)
} }
defer file.Close() defer file.Close()
feed := f.Get() channel := c.Get()
if err = gob.NewEncoder(file).Encode(feed); err != nil { if err = gob.NewEncoder(file).Encode(channel); err != nil {
return fmt.Errorf("error encoding file %v: %v", filename, err) return fmt.Errorf("error encoding file %v: %v", filename, err)
} }
return nil return nil
} }
func (f *Feed) Add(i *feeds.Item) { func (c *Channel) Add(i *rss.Item) {
f.addCh <- i c.addCh <- i
} }

View File

@ -6,8 +6,8 @@ import (
"net/http" "net/http"
"time" "time"
"git.streifling.com/jason/rss"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/gorilla/feeds"
"streifling.com/jason/cpolis/cmd/data" "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(c *data.Channel, al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
uuid, err := uuid.Parse(r.PostFormValue("uuid")) uuid, err := uuid.Parse(r.PostFormValue("uuid"))
if err != nil { if err != nil {
@ -141,14 +141,14 @@ func PublishArticle(f *data.Feed, al *data.ArticleList, s *data.CookieStore) htt
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
f.Add(&feeds.Item{ c.Add(&rss.Item{
Title: article.Title, Title: article.Title,
Author: &feeds.Author{Name: session.Values["name"].(string)}, Author: session.Values["name"].(string),
Created: article.Created, PubDate: article.Created.Format(time.RFC1123Z),
Description: article.Desc, Description: article.Desc,
Content: article.Content, Content: &rss.Content{Value: article.Content},
}) })
f.Save("tmp/rss.gob") c.Save("tmp/rss.gob")
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles("web/templates/hub.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)

View File

@ -5,13 +5,16 @@ import (
"log" "log"
"net/http" "net/http"
"git.streifling.com/jason/rss"
"streifling.com/jason/cpolis/cmd/data" "streifling.com/jason/cpolis/cmd/data"
) )
func ShowRSS(f *data.Feed) http.HandlerFunc { func ShowRSS(c *data.Channel) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
feed := f.Get() channel := c.Get()
rss, err := feed.ToRss() feed := rss.NewFeed()
feed.Channels = append(feed.Channels, &channel)
rss, err := feed.ToXML()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)

1
go.mod
View File

@ -3,6 +3,7 @@ module streifling.com/jason/cpolis
go 1.22.0 go 1.22.0
require ( require (
git.streifling.com/jason/rss v0.0.0-20240305160829-6cd08bb65d2a
github.com/go-sql-driver/mysql v1.7.1 github.com/go-sql-driver/mysql v1.7.1
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/gorilla/feeds v1.1.2 github.com/gorilla/feeds v1.1.2

6
go.sum
View File

@ -1,3 +1,9 @@
git.streifling.com/jason/rss v0.0.0-20240305152729-9d8cc6464565 h1:/eO9NTksh+9yLz3HiYNR7BJo/iMTxxW5/d9h8I6vR6E=
git.streifling.com/jason/rss v0.0.0-20240305152729-9d8cc6464565/go.mod h1:gpZF0nZbQSstMpyHD9DTAvlQEG7v4pjO5c7aIMWM4Jg=
git.streifling.com/jason/rss v0.0.0-20240305160544-c8551159fe32 h1:G25NZzsD73rOkGYgV2vPUDviB0JXk5qi+dXOwB6J56U=
git.streifling.com/jason/rss v0.0.0-20240305160544-c8551159fe32/go.mod h1:gpZF0nZbQSstMpyHD9DTAvlQEG7v4pjO5c7aIMWM4Jg=
git.streifling.com/jason/rss v0.0.0-20240305160829-6cd08bb65d2a h1:TWQ9gwe7eWjaLUrZ0CJSc+sUUOw3VoGHlR3F8mH6vqs=
git.streifling.com/jason/rss v0.0.0-20240305160829-6cd08bb65d2a/go.mod h1:gpZF0nZbQSstMpyHD9DTAvlQEG7v4pjO5c7aIMWM4Jg=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= 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/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=

View File

@ -29,10 +29,10 @@ func main() {
} }
defer db.Close() defer db.Close()
feed, err := data.OpenFeed("tmp/rss.gob") feed, err := data.LoadChannel("tmp/rss.gob")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
feed = data.NewFeed("Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt", feed = data.NewChannel("Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt",
"https://distrikt-ni-st.de", "https://distrikt-ni-st.de",
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität") "Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität")
} }