2022-05-20 23:24:52 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/gorilla/feeds"
|
|
|
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
|
2022-05-20 23:51:41 +00:00
|
|
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
|
2022-05-20 23:24:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a *api) renderFeedHandler() http.Handler {
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2022-05-20 23:51:41 +00:00
|
|
|
tag := r.FormValue("tag")
|
|
|
|
|
|
|
|
var (
|
|
|
|
posts []post.StoredPost
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if tag == "" {
|
|
|
|
posts, _, err = a.params.PostStore.Get(0, 20)
|
|
|
|
} else {
|
|
|
|
posts, err = a.params.PostStore.GetByTag(tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
apiutil.InternalServerError(rw, r, fmt.Errorf("fetching recent posts: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:24:52 +00:00
|
|
|
author := &feeds.Author{
|
|
|
|
Name: "mediocregopher",
|
|
|
|
}
|
|
|
|
|
|
|
|
publicURL := a.params.PublicURL.String()
|
|
|
|
|
|
|
|
feed := feeds.Feed{
|
|
|
|
Title: "Mediocre Blog",
|
|
|
|
Link: &feeds.Link{Href: publicURL + "/"},
|
|
|
|
Description: "A mix of tech, art, travel, and who knows what else.",
|
|
|
|
Author: author,
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:51:41 +00:00
|
|
|
for _, post := range posts {
|
2022-05-20 23:24:52 +00:00
|
|
|
|
|
|
|
if post.PublishedAt.After(feed.Updated) {
|
|
|
|
feed.Updated = post.PublishedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
if post.LastUpdatedAt.After(feed.Updated) {
|
|
|
|
feed.Updated = post.LastUpdatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
postURL := publicURL + filepath.Join("/posts", post.ID)
|
|
|
|
|
2022-05-22 15:33:24 +00:00
|
|
|
item := &feeds.Item{
|
2022-05-20 23:24:52 +00:00
|
|
|
Title: post.Title,
|
|
|
|
Link: &feeds.Link{Href: postURL},
|
|
|
|
Author: author,
|
|
|
|
Description: post.Description,
|
|
|
|
Id: postURL,
|
|
|
|
Created: post.PublishedAt,
|
2022-05-22 15:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
feed.Items = append(feed.Items, item)
|
2022-05-20 23:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := feed.WriteAtom(rw); err != nil {
|
|
|
|
apiutil.InternalServerError(rw, r, fmt.Errorf("writing atom feed: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|