diff --git a/.gitignore b/.gitignore index adf8f72..8ce089c 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ # Go workspace file go.work +# Custom stuff +tmp/ diff --git a/cmd/feed/rss.go b/cmd/feed/rss.go index 747732e..7db9488 100644 --- a/cmd/feed/rss.go +++ b/cmd/feed/rss.go @@ -1,19 +1,21 @@ package feed import ( + "encoding/gob" "fmt" + "os" "time" "github.com/gorilla/feeds" ) type Feed struct { - *feeds.Feed + feeds.Feed } -func NewFeed(title, link, desc string) Feed { - return Feed{ - Feed: &feeds.Feed{ +func NewFeed(title, link, desc string) *Feed { + return &Feed{ + Feed: feeds.Feed{ Title: title, Link: &feeds.Link{Href: link}, Description: desc, @@ -21,11 +23,40 @@ func NewFeed(title, link, desc string) Feed { } } -func AddToFeed(feed Feed, title, content string) error { +func SaveFeed(feed *Feed, filename string) error { + file, err := os.Create(filename) + if err != nil { + return fmt.Errorf("error cmd/feed/rss.go WriteFeed os.Create(): %v", err) + } + defer file.Close() + + encoder := gob.NewEncoder(file) + encoder.Encode(feed) + + return nil +} + +func OpenFeed(filename string) (*Feed, error) { + var feed *Feed + + file, err := os.Open(filename) + if err != nil { + return nil, fmt.Errorf("error cmd/feed/rss.go ReadFeed os.Open(): %v", err) + } + defer file.Close() + + decoder := gob.NewDecoder(file) + decoder.Decode(feed) + + return feed, nil +} + +func AddToFeed(feed *Feed, title, desc, content string) error { item := feeds.Item{ - Title: title, - Created: time.Now(), - Content: content, + Title: title, + Created: time.Now(), + Description: desc, + Content: content, } feed.Add(&item) diff --git a/cmd/handlers/editor.go b/cmd/handlers/editor.go index d1fa6ae..bee5b75 100644 --- a/cmd/handlers/editor.go +++ b/cmd/handlers/editor.go @@ -8,9 +8,10 @@ import ( "streifling.com/jason/cpolis/cmd/feed" ) -func HandleFinishedEdit(f feed.Feed) http.HandlerFunc { +func HandleFinishedEdit(f *feed.Feed) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { title := r.PostFormValue("editor-title") + desc := r.PostFormValue("editor-desc") mdContent := r.PostFormValue("editor-text") content, err := articles.ConvertToHTML(mdContent) @@ -19,7 +20,8 @@ func HandleFinishedEdit(f feed.Feed) http.HandlerFunc { log.Panicln(err) } - feed.AddToFeed(f, title, content) + feed.AddToFeed(f, title, desc, content) + feed.SaveFeed(f, "tmp/rss.gob") // template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem) } } diff --git a/web/templates/editor.html b/web/templates/editor.html index 86278e3..0cf807d 100644 --- a/web/templates/editor.html +++ b/web/templates/editor.html @@ -1,6 +1,7 @@ {{define "page-content"}}