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

View File

@ -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(c *data.Channel, 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,14 +141,14 @@ 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{
c.Add(&rss.Item{
Title: article.Title,
Author: &feeds.Author{Name: session.Values["name"].(string)},
Created: article.Created,
Author: session.Values["name"].(string),
PubDate: article.Created.Format(time.RFC1123Z),
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 = template.Must(tmpl, err)

View File

@ -5,13 +5,16 @@ import (
"log"
"net/http"
"git.streifling.com/jason/rss"
"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) {
feed := f.Get()
rss, err := feed.ToRss()
channel := c.Get()
feed := rss.NewFeed()
feed.Channels = append(feed.Channels, &channel)
rss, err := feed.ToXML()
if err != nil {
log.Println(err)
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
require (
git.streifling.com/jason/rss v0.0.0-20240305160829-6cd08bb65d2a
github.com/go-sql-driver/mysql v1.7.1
github.com/google/uuid v1.6.0
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/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=

View File

@ -29,10 +29,10 @@ func main() {
}
defer db.Close()
feed, err := data.OpenFeed("tmp/rss.gob")
feed, err := data.LoadChannel("tmp/rss.gob")
if err != nil {
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",
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität")
}