Changed articles and rss to channels
This commit is contained in:
parent
4b5929911e
commit
4e2cae74bb
@ -1,7 +1,6 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@ -12,30 +11,66 @@ type Article struct {
|
|||||||
Created time.Time
|
Created time.Time
|
||||||
Desc string
|
Desc string
|
||||||
Content string
|
Content string
|
||||||
|
Tags []string
|
||||||
UUID uuid.UUID
|
UUID uuid.UUID
|
||||||
}
|
}
|
||||||
|
|
||||||
type ArticleList struct {
|
type ArticleList struct {
|
||||||
List []Article
|
addCh chan *Article
|
||||||
sync.Mutex
|
delCh chan uuid.UUID
|
||||||
|
retCh chan *Article
|
||||||
|
listCh chan []Article
|
||||||
|
list []*Article
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ArticleList) Add(a Article) {
|
func (l *ArticleList) start() {
|
||||||
l.Lock()
|
for {
|
||||||
l.List = append(l.List, a)
|
select {
|
||||||
l.Unlock()
|
case article := <-l.addCh:
|
||||||
}
|
l.list = append(l.list, article)
|
||||||
|
case uuid := <-l.delCh:
|
||||||
func (l *ArticleList) Release(uuid uuid.UUID) (Article, bool) {
|
for i, article := range l.list {
|
||||||
l.Lock()
|
if article.UUID == uuid {
|
||||||
for i, article := range l.List {
|
l.list = append(l.list[:i], l.list[i+1:]...)
|
||||||
if article.UUID == uuid {
|
l.retCh <- article
|
||||||
foo := l.List[i]
|
}
|
||||||
l.List = append(l.List[:i], l.List[i+1:]...)
|
}
|
||||||
l.Unlock()
|
case l.listCh <- func() []Article {
|
||||||
return foo, true
|
var list []Article
|
||||||
|
for _, article := range l.list {
|
||||||
|
list = append(list, *article)
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}():
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
l.Unlock()
|
}
|
||||||
return Article{}, false
|
|
||||||
|
func NewArticleList() *ArticleList {
|
||||||
|
list := &ArticleList{
|
||||||
|
addCh: make(chan *Article),
|
||||||
|
delCh: make(chan uuid.UUID),
|
||||||
|
retCh: make(chan *Article),
|
||||||
|
listCh: make(chan []Article),
|
||||||
|
list: []*Article{},
|
||||||
|
}
|
||||||
|
go list.start()
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *ArticleList) Add(a *Article) {
|
||||||
|
l.addCh <- a
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *ArticleList) Release(uuid uuid.UUID) (*Article, bool) {
|
||||||
|
l.delCh <- uuid
|
||||||
|
article := <-l.retCh
|
||||||
|
if article == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
return article, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *ArticleList) List() []Article {
|
||||||
|
return <-l.listCh
|
||||||
}
|
}
|
||||||
|
@ -4,40 +4,66 @@ import (
|
|||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/gorilla/feeds"
|
"github.com/gorilla/feeds"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Feed struct {
|
type Feed struct {
|
||||||
feeds.Feed
|
addCh chan *feeds.Item
|
||||||
sync.Mutex
|
openCh chan feeds.Feed
|
||||||
|
saveCh chan feeds.Feed
|
||||||
|
showCh chan feeds.Feed
|
||||||
|
feed feeds.Feed
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Feed) start() {
|
||||||
|
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:
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFeed(title, link, desc string) *Feed {
|
func NewFeed(title, link, desc string) *Feed {
|
||||||
return &Feed{
|
feed := &Feed{
|
||||||
Feed: feeds.Feed{
|
addCh: make(chan *feeds.Item),
|
||||||
|
openCh: make(chan feeds.Feed),
|
||||||
|
saveCh: make(chan feeds.Feed),
|
||||||
|
showCh: make(chan feeds.Feed),
|
||||||
|
feed: feeds.Feed{
|
||||||
Title: title,
|
Title: title,
|
||||||
Link: &feeds.Link{Href: link},
|
Link: &feeds.Link{Href: link},
|
||||||
Description: desc,
|
Description: desc,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
go feed.start()
|
||||||
|
return feed
|
||||||
}
|
}
|
||||||
|
|
||||||
func OpenFeed(filename string) (*Feed, error) {
|
func OpenFeed(filename string) (*Feed, error) {
|
||||||
|
feed := new(Feed)
|
||||||
|
go feed.start()
|
||||||
|
|
||||||
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 := new(Feed)
|
|
||||||
decoder := gob.NewDecoder(file)
|
decoder := gob.NewDecoder(file)
|
||||||
err = decoder.Decode(&feed.Feed)
|
tmpFeed := new(feeds.Feed)
|
||||||
|
err = decoder.Decode(tmpFeed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error decoding file %v: %v", filename, err)
|
return nil, fmt.Errorf("error decoding file %v: %v", filename, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
feed.openCh <- *tmpFeed
|
||||||
|
|
||||||
return feed, nil
|
return feed, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,9 +75,8 @@ func (f *Feed) Save(filename string) error {
|
|||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
encoder := gob.NewEncoder(file)
|
encoder := gob.NewEncoder(file)
|
||||||
f.Lock()
|
feed := <-f.saveCh
|
||||||
err = encoder.Encode(f.Feed)
|
err = encoder.Encode(feed)
|
||||||
f.Unlock()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error encoding file %v: %v", filename, err)
|
return fmt.Errorf("error encoding file %v: %v", filename, err)
|
||||||
}
|
}
|
||||||
@ -60,7 +85,9 @@ func (f *Feed) Save(filename string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *Feed) Add(i *feeds.Item) {
|
func (f *Feed) Add(i *feeds.Item) {
|
||||||
f.Lock()
|
f.addCh <- i
|
||||||
f.Items = append(f.Items, i)
|
}
|
||||||
f.Unlock()
|
|
||||||
|
func (f *Feed) Show() feeds.Feed {
|
||||||
|
return <-f.showCh
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ func WriteArticle() http.HandlerFunc {
|
|||||||
|
|
||||||
func FinishArticle(l *data.ArticleList) http.HandlerFunc {
|
func FinishArticle(l *data.ArticleList) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
article := data.Article{}
|
article := new(data.Article)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
article.Title, err = data.ConvertToPlain(r.PostFormValue("editor-title"))
|
article.Title, err = data.ConvertToPlain(r.PostFormValue("editor-title"))
|
||||||
@ -73,7 +73,7 @@ func ReviewArticle(l *data.ArticleList) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, article := range l.List {
|
for _, article := range l.List() {
|
||||||
if article.UUID == uuid {
|
if article.UUID == uuid {
|
||||||
template.Must(template.ParseFiles("web/templates/to-be-published.html")).ExecuteTemplate(w, "page-content", article)
|
template.Must(template.ParseFiles("web/templates/to-be-published.html")).ExecuteTemplate(w, "page-content", article)
|
||||||
return
|
return
|
||||||
|
@ -8,8 +8,9 @@ import (
|
|||||||
"streifling.com/jason/cpolis/cmd/data"
|
"streifling.com/jason/cpolis/cmd/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ShowRSS(feed *data.Feed) 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) {
|
||||||
|
feed := f.Show()
|
||||||
rss, err := feed.ToRss()
|
rss, err := feed.ToRss()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
2
main.go
2
main.go
@ -31,7 +31,7 @@ func main() {
|
|||||||
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität")
|
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität")
|
||||||
}
|
}
|
||||||
|
|
||||||
articleList := new(data.ArticleList)
|
articleList := data.NewArticleList()
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
|
mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user