diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-07-12 23:06:42 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-07-12 23:06:42 -0400 |
| commit | 2734c671324e980ef7424e367461ebaa4e0034a5 (patch) | |
| tree | d4ee457c18002011b9e5a5927baa34e5b6d7b16c /cmd/server/feed.go | |
| parent | 3f47f136de6a79adedbe16329586944c6a90ee65 (diff) | |
| download | go-website-2734c671324e980ef7424e367461ebaa4e0034a5.tar.gz go-website-2734c671324e980ef7424e367461ebaa4e0034a5.tar.xz | |
Add atom feed to site, require config.yml
Diffstat (limited to 'cmd/server/feed.go')
| -rw-r--r-- | cmd/server/feed.go | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/cmd/server/feed.go b/cmd/server/feed.go index 278c706..2d4a75b 100644 --- a/cmd/server/feed.go +++ b/cmd/server/feed.go @@ -7,6 +7,7 @@ import ( "fmt" "log" "net/http" + "strconv" "strings" "time" @@ -30,7 +31,7 @@ type Link struct { type Content struct { Type string `xml:"type,attr"` - Data []byte `xml:"chardata"` + Data string `xml:",chardata"` } type Entry struct { @@ -107,8 +108,31 @@ func (a Atom) MarshalXML(e *xml.Encoder, start xml.StartElement) error { return e.EncodeElement(a2, start) } +// FeedHandler takes care of pulling from the index all of the relevant posts +// and dumping them into an Atom feed. +// +// Relevant query parameters are: +// +// "content" if unset, or set to false content is omitted from the feed +// "limit=n" stop at "n" and return the feed +// func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) + var addContent bool + var limit int + + if _, ok := r.URL.Query()["content"]; ok { + if r.URL.Query().Get("content") != "false" { + addContent = true + } + } + + if l := r.URL.Query().Get("limit"); l != "" { + i, err := strconv.Atoi(l) + if err == nil { + limit = i + } + } tag, ok := vars["tag"] if !ok { @@ -130,6 +154,12 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { return } + pages, dateless := pages.RemoveDateless() + for _, p := range dateless { + log.Printf("Warning, page %s has no Date field. Skipping inclusion on feed", p) + } + pages.SortDate() + feed := &Atom{ Author: a.Author, Title: a.Title, @@ -140,11 +170,9 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { entries := []Entry{} - for _, p := range pages { - - if p.Date == nil { - log.Printf("Warning, page %s has no Date field. Skipping inclusion on feed", p) - continue + for n, p := range pages { + if limit != 0 && n >= limit { + break } content := &bytes.Buffer{} @@ -155,12 +183,18 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { return } - entries = append(entries, Entry{ + entry := Entry{ Title: p.Title, Updated: &p.Date.Time, Links: []Link{Link{Href: strings.Join([]string{a.SiteURL, p.Path()}, "/")}}, - // Content: Content{Type: "html", Data: content.Bytes()}, - }) + } + + if addContent { + entry.Content = &Content{Type: "html", Data: content.String()} + } + + entries = append(entries, entry) + } feed.Entries = entries |
