Refactor RSSFeedHandler function to generate RSS feed with dynamic title and link
This commit is contained in:
parent
caa3833178
commit
b975ddb9e5
1 changed files with 6 additions and 8 deletions
14
api/rss.go
14
api/rss.go
|
@ -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)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue