Added description and a way to save and restore the RSS feed.
This commit is contained in:
parent
ee04a2a351
commit
c5623fe4fd
2
.gitignore
vendored
2
.gitignore
vendored
@ -21,3 +21,5 @@
|
|||||||
# Go workspace file
|
# Go workspace file
|
||||||
go.work
|
go.work
|
||||||
|
|
||||||
|
# Custom stuff
|
||||||
|
tmp/
|
||||||
|
@ -1,19 +1,21 @@
|
|||||||
package feed
|
package feed
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/gob"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/feeds"
|
"github.com/gorilla/feeds"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Feed struct {
|
type Feed struct {
|
||||||
*feeds.Feed
|
feeds.Feed
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFeed(title, link, desc string) Feed {
|
func NewFeed(title, link, desc string) *Feed {
|
||||||
return Feed{
|
return &Feed{
|
||||||
Feed: &feeds.Feed{
|
Feed: feeds.Feed{
|
||||||
Title: title,
|
Title: title,
|
||||||
Link: &feeds.Link{Href: link},
|
Link: &feeds.Link{Href: link},
|
||||||
Description: desc,
|
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{
|
item := feeds.Item{
|
||||||
Title: title,
|
Title: title,
|
||||||
Created: time.Now(),
|
Created: time.Now(),
|
||||||
Content: content,
|
Description: desc,
|
||||||
|
Content: content,
|
||||||
}
|
}
|
||||||
feed.Add(&item)
|
feed.Add(&item)
|
||||||
|
|
||||||
|
@ -8,9 +8,10 @@ import (
|
|||||||
"streifling.com/jason/cpolis/cmd/feed"
|
"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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
title := r.PostFormValue("editor-title")
|
title := r.PostFormValue("editor-title")
|
||||||
|
desc := r.PostFormValue("editor-desc")
|
||||||
mdContent := r.PostFormValue("editor-text")
|
mdContent := r.PostFormValue("editor-text")
|
||||||
|
|
||||||
content, err := articles.ConvertToHTML(mdContent)
|
content, err := articles.ConvertToHTML(mdContent)
|
||||||
@ -19,7 +20,8 @@ func HandleFinishedEdit(f feed.Feed) http.HandlerFunc {
|
|||||||
log.Panicln(err)
|
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)
|
// template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
{{define "page-content"}}
|
{{define "page-content"}}
|
||||||
<form>
|
<form>
|
||||||
<input type="text" name="editor-title" value="Titel">
|
<input type="text" name="editor-title" value="Titel">
|
||||||
|
<textarea name="editor-desc"></textarea>
|
||||||
<textarea name="editor-text"></textarea>
|
<textarea name="editor-text"></textarea>
|
||||||
<input type="submit" value="Senden" hx-post="/finished-edit/" hx-target="#page-content">
|
<input type="submit" value="Senden" hx-post="/finished-edit/" hx-target="#page-content">
|
||||||
</form>
|
</form>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user