Created func for minimum spec for rss and article structs, thereby crushing an annoying bug that was caused by not initializing channels but waiting for messages to go through them

This commit is contained in:
Jason Streifling 2024-03-02 00:28:42 +01:00
parent 4e2cae74bb
commit 2da17014e4
4 changed files with 70 additions and 46 deletions

View File

@ -1,6 +1,7 @@
package data
import (
"sync"
"time"
"github.com/google/uuid"
@ -19,11 +20,22 @@ type ArticleList struct {
addCh chan *Article
delCh chan uuid.UUID
retCh chan *Article
listCh chan []Article
getCh chan []Article
list []*Article
wg sync.WaitGroup
}
func minArticleList() *ArticleList {
return &ArticleList{
addCh: make(chan *Article),
delCh: make(chan uuid.UUID),
retCh: make(chan *Article),
getCh: make(chan []Article),
}
}
func (l *ArticleList) start() {
l.wg.Done()
for {
select {
case article := <-l.addCh:
@ -35,7 +47,7 @@ func (l *ArticleList) start() {
l.retCh <- article
}
}
case l.listCh <- func() []Article {
case l.getCh <- func() []Article {
var list []Article
for _, article := range l.list {
list = append(list, *article)
@ -47,14 +59,13 @@ func (l *ArticleList) start() {
}
func NewArticleList() *ArticleList {
list := &ArticleList{
addCh: make(chan *Article),
delCh: make(chan uuid.UUID),
retCh: make(chan *Article),
listCh: make(chan []Article),
list: []*Article{},
}
list := minArticleList()
list.list = []*Article{}
list.wg.Add(1)
go list.start()
list.wg.Wait()
return list
}
@ -71,6 +82,6 @@ func (l *ArticleList) Release(uuid uuid.UUID) (*Article, bool) {
return article, true
}
func (l *ArticleList) List() []Article {
return <-l.listCh
func (l *ArticleList) Get() []Article {
return <-l.getCh
}

View File

@ -4,57 +4,74 @@ import (
"encoding/gob"
"fmt"
"os"
"sync"
"github.com/gorilla/feeds"
)
type Feed struct {
addCh chan *feeds.Item
openCh chan feeds.Feed
saveCh chan feeds.Feed
showCh chan feeds.Feed
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.saveCh <- f.feed:
case tmpFeed := <-f.openCh:
f.feed = tmpFeed
case f.showCh <- f.feed:
case f.getCh <- f.feed:
case f.feed = <-f.setCh:
}
}
}
func NewFeed(title, link, desc string) *Feed {
feed := &Feed{
addCh: make(chan *feeds.Item),
openCh: make(chan feeds.Feed),
saveCh: make(chan feeds.Feed),
showCh: make(chan feeds.Feed),
feed: feeds.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 OpenFeed(filename string) (*Feed, error) {
feed := new(Feed)
go feed.start()
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)
@ -62,7 +79,7 @@ func OpenFeed(filename string) (*Feed, error) {
return nil, fmt.Errorf("error decoding file %v: %v", filename, err)
}
feed.openCh <- *tmpFeed
feed.Set(*tmpFeed)
return feed, nil
}
@ -75,7 +92,7 @@ func (f *Feed) Save(filename string) error {
defer file.Close()
encoder := gob.NewEncoder(file)
feed := <-f.saveCh
feed := f.Get()
err = encoder.Encode(feed)
if err != nil {
return fmt.Errorf("error encoding file %v: %v", filename, err)
@ -87,7 +104,3 @@ func (f *Feed) Save(filename string) error {
func (f *Feed) Add(i *feeds.Item) {
f.addCh <- i
}
func (f *Feed) Show() feeds.Feed {
return <-f.showCh
}

View File

@ -60,7 +60,7 @@ func FinishArticle(l *data.ArticleList) http.HandlerFunc {
func ShowUnpublishedArticles(l *data.ArticleList) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/unpublished-articles.html")).ExecuteTemplate(w, "page-content", l.List)
template.Must(template.ParseFiles("web/templates/unpublished-articles.html")).ExecuteTemplate(w, "page-content", l.Get())
}
}
@ -73,7 +73,7 @@ func ReviewArticle(l *data.ArticleList) http.HandlerFunc {
return
}
for _, article := range l.List() {
for _, article := range l.Get() {
if article.UUID == uuid {
template.Must(template.ParseFiles("web/templates/to-be-published.html")).ExecuteTemplate(w, "page-content", article)
return

View File

@ -10,7 +10,7 @@ import (
func ShowRSS(f *data.Feed) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
feed := f.Show()
feed := f.Get()
rss, err := feed.ToRss()
if err != nil {
log.Println(err)