Template
1
0
Fork 0

Refactor RSSFeedHandler function to generate RSS feed with dynamic title and link

This commit is contained in:
Atridad Lahiji 2024-02-08 00:10:36 -07:00
parent caa3833178
commit b975ddb9e5
No known key found for this signature in database

View file

@ -1,7 +1,6 @@
package api package api
import ( import (
"fmt"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -13,11 +12,6 @@ import (
) )
func RSSFeedHandler(c echo.Context) error { func RSSFeedHandler(c echo.Context) error {
feed := &feeds.Feed{
Title: "Goth Stack Sample Blog",
Link: &feeds.Link{Href: "https://goth-stack.fly.dev/"},
}
files, err := os.ReadDir("./content/") files, err := os.ReadDir("./content/")
protocol := "http" protocol := "http"
@ -25,6 +19,11 @@ func RSSFeedHandler(c echo.Context) error {
protocol = "https" protocol = "https"
} }
feed := &feeds.Feed{
Title: "GOTH Stack Demo Blog",
Link: &feeds.Link{Href: protocol + "://" + c.Request().Host + "/api/rss"},
}
if err != nil { if err != nil {
http.Error(c.Response().Writer, "There was an issue finding posts!", http.StatusInternalServerError) http.Error(c.Response().Writer, "There was an issue finding posts!", http.StatusInternalServerError)
return nil return nil
@ -47,6 +46,5 @@ func RSSFeedHandler(c echo.Context) error {
} }
rss, _ := feed.ToRss() rss, _ := feed.ToRss()
fmt.Println("RSS:", rss) return c.Blob(http.StatusOK, "application/rss+xml", []byte(rss))
return c.String(http.StatusOK, rss)
} }