owl-blogs/web/rss_handler.go

102 lines
2.2 KiB
Go
Raw Normal View History

2023-06-25 19:32:36 +00:00
package web
import (
2023-07-20 17:49:52 +00:00
"bytes"
"encoding/xml"
"net/url"
2023-06-25 19:32:36 +00:00
"owl-blogs/app"
2023-07-20 17:49:52 +00:00
"owl-blogs/app/repository"
"owl-blogs/domain/model"
"sort"
"time"
2023-06-25 19:32:36 +00:00
"github.com/gofiber/fiber/v2"
)
2023-07-20 17:49:52 +00:00
type RSS struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
Channel RSSChannel `xml:"channel"`
}
type RSSChannel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Items []RSSItem `xml:"item"`
}
type RSSItem struct {
Guid string `xml:"guid"`
Title string `xml:"title"`
Link string `xml:"link"`
PubDate string `xml:"pubDate"`
Description string `xml:"description"`
}
func RenderRSSFeed(config model.SiteConfig, entries []model.Entry) (string, error) {
rss := RSS{
Version: "2.0",
Channel: RSSChannel{
Title: config.Title,
Link: config.FullUrl,
Description: config.SubTitle,
Items: make([]RSSItem, 0),
},
}
for _, entry := range entries {
content := entry.Content()
url, _ := url.JoinPath(config.FullUrl, "/posts/", entry.ID())
rss.Channel.Items = append(rss.Channel.Items, RSSItem{
Guid: url,
Title: entry.Title(),
Link: url,
PubDate: entry.PublishedAt().Format(time.RFC1123Z),
Description: string(content),
})
}
buf := new(bytes.Buffer)
err := xml.NewEncoder(buf).Encode(rss)
if err != nil {
return "", err
}
return xml.Header + buf.String(), nil
}
2023-06-25 19:32:36 +00:00
type RSSHandler struct {
2023-07-20 17:49:52 +00:00
configRepo repository.ConfigRepository
entrySvc *app.EntryService
2023-06-25 19:32:36 +00:00
}
2023-07-20 17:49:52 +00:00
func NewRSSHandler(entryService *app.EntryService, configRepo repository.ConfigRepository) *RSSHandler {
return &RSSHandler{entrySvc: entryService, configRepo: configRepo}
2023-06-25 19:32:36 +00:00
}
func (h *RSSHandler) Handle(c *fiber.Ctx) error {
2023-07-20 17:49:52 +00:00
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationXML)
siteConfig := getSiteConfig(h.configRepo)
entries, err := h.entrySvc.FindAllByType(&siteConfig.PrimaryListInclude, true, false)
if err != nil {
return err
}
// sort entries by date descending
sort.Slice(entries, func(i, j int) bool {
return entries[i].PublishedAt().After(*entries[j].PublishedAt())
})
rss, err := RenderRSSFeed(siteConfig, entries)
if err != nil {
return err
}
return c.SendString(rss)
2023-06-25 19:32:36 +00:00
}