Template
1
0
Fork 0
goth.stack/api/rss.go

64 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-04-16 00:09:09 -06:00
package api
2023-05-18 20:04:55 -06:00
2024-10-20 16:16:50 -06:00
// RSSFeedHandler godoc
// @Summary Get RSS feed
// @Description Returns an RSS feed of blog posts
// @Tags rss
// @Accept json
// @Produce xml
// @Success 200 {string} string "RSS feed content"
// @Failure 500 {string} string "Internal Server Error"
// @Router /rss [get]
2023-05-18 20:04:55 -06:00
import (
"io/fs"
"net/http"
"strings"
"time"
"github.com/gorilla/feeds"
"github.com/labstack/echo/v4"
2024-11-03 17:01:48 -06:00
contentfs "goth.stack/content"
"goth.stack/lib"
2023-05-18 20:04:55 -06:00
)
func RSSFeedHandler(c echo.Context) error {
files, err := fs.ReadDir(contentfs.FS, ".")
protocol := "http"
if c.Request().TLS != nil {
protocol = "https"
}
feed := &feeds.Feed{
Title: "Atridad Lahiji's Blog",
Link: &feeds.Link{Href: protocol + "://" + c.Request().Host + "/api/rss"},
}
if err != nil {
http.Error(c.Response().Writer, "There was an issue finding posts!", http.StatusInternalServerError)
return nil
}
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".md") {
frontMatter, err := lib.ExtractFrontMatter(file, contentfs.FS)
if err != nil {
http.Error(c.Response().Writer, "There was an issue rendering the posts!", http.StatusInternalServerError)
return nil
}
date, _ := time.Parse("January 2 2006", frontMatter.Date)
feed.Add(&feeds.Item{
Title: frontMatter.Name,
Link: &feeds.Link{Href: protocol + "://" + c.Request().Host + "/post/" + strings.TrimSuffix(file.Name(), ".md")},
Created: date,
})
}
}
rss, _ := feed.ToRss()
return c.Blob(http.StatusOK, "application/rss+xml", []byte(rss))
}