Compare commits

..

No commits in common. "e4f085f76283b7453ba00ba5eac6af5de66acce4" and "21170c9cc2be4eb03357f723f738b6f764de6ed9" have entirely different histories.

7 changed files with 63 additions and 84 deletions

View File

@ -21,17 +21,15 @@ 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
getCh chan []Article
retCh chan *Article retCh chan *Article
getCh 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
@ -43,8 +41,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),
getCh: make(chan []Article),
retCh: make(chan *Article), retCh: make(chan *Article),
getCh: make(chan []Article),
} }
} }
@ -92,7 +90,7 @@ func (tl *TagList) start() {
func NewArticleList() *ArticleList { func NewArticleList() *ArticleList {
list := initArticleList() list := initArticleList()
list.articles = make([]*Article, 0) list.articles = []*Article{}
list.wg.Add(1) list.wg.Add(1)
go list.start() go list.start()
@ -150,7 +148,7 @@ func LoadArticleList(filename string) (*ArticleList, error) {
func NewTagList() *TagList { func NewTagList() *TagList {
list := initTagList() list := initTagList()
list.tags = make([]string, 0) list.tags = []string{}
list.wg.Add(1) list.wg.Add(1)
go list.start() go list.start()

View File

@ -6,99 +6,96 @@ import (
"os" "os"
"sync" "sync"
"git.streifling.com/jason/rss" "github.com/gorilla/feeds"
) )
type Channel struct { type Feed struct {
addCh chan *rss.Item addCh chan *feeds.Item
setCh chan rss.Channel setCh chan feeds.Feed
getCh chan rss.Channel getCh chan feeds.Feed
channel rss.Channel feed feeds.Feed
wg sync.WaitGroup wg sync.WaitGroup
} }
func initChannel() *Channel { func initFeed() *Feed {
return &Channel{ return &Feed{
addCh: make(chan *rss.Item), addCh: make(chan *feeds.Item),
setCh: make(chan rss.Channel), setCh: make(chan feeds.Feed),
getCh: make(chan rss.Channel), getCh: make(chan feeds.Feed),
channel: rss.Channel{
Items: make([]*rss.Item, 0),
},
} }
} }
func (c *Channel) start() { func (f *Feed) start() {
c.wg.Done() f.wg.Done()
for { for {
select { select {
case item := <-c.addCh: case item := <-f.addCh:
c.channel.Items = append(c.channel.Items, item) f.feed.Items = append(f.feed.Items, item)
case c.getCh <- c.channel: case f.getCh <- f.feed:
case c.channel = <-c.setCh: case f.feed = <-f.setCh:
} }
} }
} }
func NewChannel(title, link, desc string) *Channel { func NewFeed(title, link, desc string) *Feed {
channel := initChannel() feed := initFeed()
channel.channel = rss.Channel{ feed.feed = feeds.Feed{
Title: title, Title: title,
Link: link, Link: &feeds.Link{Href: link},
Description: desc, Description: desc,
} }
channel.wg.Add(1) feed.wg.Add(1)
go channel.start() go feed.start()
channel.wg.Wait() feed.wg.Wait()
return channel return feed
} }
func (c *Channel) Get() rss.Channel { func (f *Feed) Get() feeds.Feed {
return <-c.getCh return <-f.getCh
} }
func (f *Channel) Set(channel rss.Channel) { func (f *Feed) Set(feed feeds.Feed) {
f.setCh <- channel f.setCh <- feed
} }
func LoadChannel(filename string) (*Channel, error) { func OpenFeed(filename string) (*Feed, 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()
channel := initChannel() feed := initFeed()
channel.wg.Add(1) feed.wg.Add(1)
go channel.start() go feed.start()
channel.wg.Wait() feed.wg.Wait()
tmpChannel := new(rss.Channel) tmpFeed := new(feeds.Feed)
if err = gob.NewDecoder(file).Decode(tmpChannel); err != nil { if err = gob.NewDecoder(file).Decode(tmpFeed); err != nil {
return nil, fmt.Errorf("error decoding channel from file %v: %v", filename, err) return nil, fmt.Errorf("error decoding file %v: %v", filename, err)
} }
channel.Set(*tmpChannel) feed.Set(*tmpFeed)
return channel, nil return feed, nil
} }
func (c *Channel) Save(filename string) error { func (f *Feed) 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()
channel := c.Get() feed := f.Get()
if err = gob.NewEncoder(file).Encode(channel); err != nil { if err = gob.NewEncoder(file).Encode(feed); 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 (c *Channel) Add(i *rss.Item) { func (f *Feed) Add(i *feeds.Item) {
c.addCh <- i f.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,9 +58,6 @@ 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")
@ -74,7 +71,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/unpublished-articles.gob") al.Save("tmp/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)
@ -119,7 +116,7 @@ func ReviewArticle(al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
} }
} }
func PublishArticle(c *data.Channel, al *data.ArticleList, s *data.CookieStore) http.HandlerFunc { func PublishArticle(f *data.Feed, 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 {
@ -144,15 +141,14 @@ func PublishArticle(c *data.Channel, al *data.ArticleList, s *data.CookieStore)
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
c.Add(&rss.Item{ f.Add(&feeds.Item{
Title: article.Title, Title: article.Title,
Author: session.Values["name"].(string), Author: &feeds.Author{Name: session.Values["name"].(string)},
PubDate: article.Created.Format(time.RFC1123Z), Created: article.Created,
Description: article.Desc, Description: article.Desc,
Content: &rss.Content{Value: article.Content}, Content: article.Content,
Categories: article.Tags,
}) })
c.Save("tmp/rss.gob") f.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,16 +5,13 @@ 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(c *data.Channel) http.HandlerFunc { func ShowRSS(f *data.Feed) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
channel := c.Get() feed := f.Get()
feed := rss.NewFeed() rss, err := feed.ToRss()
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,7 +3,6 @@ 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,11 +1,3 @@
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.LoadChannel("tmp/rss.gob") feed, err := data.OpenFeed("tmp/rss.gob")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
feed = data.NewChannel("Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt", feed = data.NewFeed("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/unpublished-articles.gob") articleList, err := data.LoadArticleList("tmp/articles.gob")
if err != nil { if err != nil {
articleList = data.NewArticleList() articleList = data.NewArticleList()
} }