diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-07 13:31:23 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-07 13:31:23 -0500 |
| commit | ca33a035c779ae14fb6330c8801c75f49dd1bb79 (patch) | |
| tree | deaabaf15d6d91079a68f247e46070399e4343ee /cmd/server/feed.go | |
| parent | 97dd660925434be537cd9a49a1d0c893b223e357 (diff) | |
| download | go-website-ca33a035c779ae14fb6330c8801c75f49dd1bb79.tar.gz go-website-ca33a035c779ae14fb6330c8801c75f49dd1bb79.tar.xz | |
Add an internal caching option. It performs quite well.v0.0.22
Also refactor and clean up most linter warnings.
Diffstat (limited to 'cmd/server/feed.go')
| -rw-r--r-- | cmd/server/feed.go | 84 |
1 files changed, 46 insertions, 38 deletions
diff --git a/cmd/server/feed.go b/cmd/server/feed.go index 7e36cb3..822d13a 100644 --- a/cmd/server/feed.go +++ b/cmd/server/feed.go @@ -16,7 +16,7 @@ import ( type Author struct { Name string `xml:"name"` // Required - Uri string `xml:"uri,omitempty"` //nolint:golint,stylecheck + Uri string `xml:"uri,omitempty"` //nolint:revive,stylecheck Email string `xml:"email,omitempty"` } @@ -36,7 +36,7 @@ type Content struct { type Entry struct { // Spec requires this, autogenerated from Title and updated if otherwise // left empty - Id string `xml:"id"` //nolint:golint,stylecheck + Id string `xml:"id"` //nolint:revive,stylecheck Title string `xml:"title"` // Required Updated *time.Time `xml:"updated"` // Required @@ -68,10 +68,10 @@ func (i Entry) MarshalXML(e *xml.Encoder, start xml.StartElement) error { i2 := (*Alias)(&i) - return e.EncodeElement(i2, start) + return e.EncodeElement(i2, start) //nolint:wrapcheck } -//nolint:stylecheck,golint +//nolint:stylecheck,revive type Atom struct { Ns string `xml:"xmlns,attr"` Title string `xml:"title"` // Required @@ -109,7 +109,7 @@ func (a Atom) MarshalXML(e *xml.Encoder, start xml.StartElement) error { a2 := (*Alias)(&a) - return e.EncodeElement(a2, start) + return e.EncodeElement(a2, start) //nolint:wrapcheck } // FeedHandler takes care of pulling from the index all of the relevant posts @@ -119,8 +119,8 @@ func (a Atom) MarshalXML(e *xml.Encoder, start xml.StartElement) error { // // "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() http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { //nolint:funlen +func (a *App) FeedHandler() http.Handler { //nolint:funlen,gocognit + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var ( addContent bool limit int @@ -179,40 +179,10 @@ func (a *App) FeedHandler() http.Handler { break } - if !p.Published { - continue - } - - content := &bytes.Buffer{} - - err := p.Render(content) + err = a.addPageToEntries(&entries, p, addContent) if err != nil { - log.Println(err) http.Error(w, "Internal server error", http.StatusInternalServerError) - - return - } - - entry := Entry{ - Title: p.Title(), - Updated: &p.Date.Time, - Links: []Link{{Href: strings.Join([]string{a.SiteURL, p.Path()}, "/")}}, - } - - if p.AuthorName != "" { - entry.Author = &Author{ - Name: p.AuthorName, - } - if p.AuthorEmail != "" { - entry.Author.Email = p.AuthorEmail - } - } - - if addContent { - entry.Content = &Content{Type: "html", Data: content.String()} } - - entries = append(entries, entry) } feed.Entries = entries @@ -237,3 +207,41 @@ func (a *App) FeedHandler() http.Handler { } }) } + +func (a *App) addPageToEntries(target *[]Entry, p *page.Page, addContent bool) error { + if !p.Published { + return nil + } + + content := &bytes.Buffer{} + + err := p.Render(content) + if err != nil { + log.Println(err) + + return err //nolint:wrapcheck + } + + entry := Entry{ + Title: p.Title(), + Updated: &p.Date.Time, + Links: []Link{{Href: strings.Join([]string{a.SiteURL, p.Path()}, "/")}}, + } + + if p.AuthorName != "" { + entry.Author = &Author{ + Name: p.AuthorName, + } + if p.AuthorEmail != "" { + entry.Author.Email = p.AuthorEmail + } + } + + if addContent { + entry.Content = &Content{Type: "html", Data: content.String()} + } + + *target = append(*target, entry) + + return nil +} |
