Compare commits

...

2 Commits

7 changed files with 84 additions and 63 deletions

View File

@ -21,15 +21,17 @@ type Article struct {
AuthorID int64 AuthorID int64
} }
// TODO: setCh
type ArticleList struct { type ArticleList struct {
addCh chan *Article addCh chan *Article
delCh chan uuid.UUID delCh chan uuid.UUID
retCh chan *Article
getCh chan []Article getCh chan []Article
retCh chan *Article
articles []*Article articles []*Article
wg sync.WaitGroup wg sync.WaitGroup
} }
// TODO: setCh
type TagList struct { type TagList struct {
addCh chan string addCh chan string
getCh chan []string getCh chan []string
@ -41,8 +43,8 @@ func initArticleList() *ArticleList {
return &ArticleList{ return &ArticleList{
addCh: make(chan *Article), addCh: make(chan *Article),
delCh: make(chan uuid.UUID), delCh: make(chan uuid.UUID),
retCh: make(chan *Article),
getCh: make(chan []Article), getCh: make(chan []Article),
retCh: make(chan *Article),
} }
} }
@ -90,7 +92,7 @@ func (tl *TagList) start() {
func NewArticleList() *ArticleList { func NewArticleList() *ArticleList {
list := initArticleList() list := initArticleList()
list.articles = []*Article{} list.articles = make([]*Article, 0)
list.wg.Add(1) list.wg.Add(1)
go list.start() go list.start()
@ -148,7 +150,7 @@ func LoadArticleList(filename string) (*ArticleList, error) {
func NewTagList() *TagList { func NewTagList() *TagList {
list := initTagList() list := initTagList()
list.tags = []string{} list.tags = make([]string, 0)
list.wg.Add(1) list.wg.Add(1)
go list.start() go list.start()

View File

@ -6,96 +6,99 @@ 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),
channel: rss.Channel{
Items: make([]*rss.Item, 0),
},
} }
} }
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"
) )
@ -58,6 +58,9 @@ func FinishArticle(al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
return return
} }
r.ParseForm()
article.Tags = append(article.Tags, r.Form["tags"]...)
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles("web/templates/login.html")
@ -71,7 +74,7 @@ func FinishArticle(al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
article.AuthorID = session.Values["id"].(int64) article.AuthorID = session.Values["id"].(int64)
al.Add(article) al.Add(article)
al.Save("tmp/articles.gob") al.Save("tmp/unpublished-articles.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)
@ -116,7 +119,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 +144,15 @@ 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},
Categories: article.Tags,
}) })
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-20240305164907-524bf9676188
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

8
go.sum
View File

@ -1,3 +1,11 @@
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=
git.streifling.com/jason/rss v0.0.0-20240305164907-524bf9676188 h1:C8M/j3f+cl5Y7YfGpU/ynb/SC/4tTYMDsyGFt3rswM8=
git.streifling.com/jason/rss v0.0.0-20240305164907-524bf9676188/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")
} }
@ -47,7 +47,7 @@ func main() {
} }
store := data.NewCookieStore(key) store := data.NewCookieStore(key)
articleList, err := data.LoadArticleList("tmp/articles.gob") articleList, err := data.LoadArticleList("tmp/unpublished-articles.gob")
if err != nil { if err != nil {
articleList = data.NewArticleList() articleList = data.NewArticleList()
} }