added RSS feed

This commit is contained in:
2022-05-08 15:00:30 -06:00
parent d5799a888d
commit cde073a274
2 changed files with 49 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.Rawley Fow
import (
"net/http"
"text/template"
"github.com/gin-gonic/gin"
"github.com/rawleyfowler/rawleydotxyz/repos"
@@ -40,6 +41,8 @@ func RegisterBlogGroup(r *gin.RouterGroup) {
c := NewBlogController(repos.NewBlogRepo("dsn"))
r.GET("/", c.IndexBlogPage)
r.GET("/post/:url", c.IndividualBlogPage)
// RSS Feed
r.GET("/feed", c.RSSFeed)
}
func (bc *BlogController) IndexBlogPage(c *gin.Context) {
@@ -60,3 +63,22 @@ func (bc *BlogController) IndividualBlogPage(c *gin.Context) {
}
c.HTML(http.StatusOK, "blog_post.tmpl", post)
}
func (bc *BlogController) RSSFeed(c *gin.Context) {
posts, err := bc.Repository.GetAllBlogPosts()
if err != nil {
c.HTML(http.StatusInternalServerError, "internal_server_error.tmpl", &gin.H{})
return
}
t, err := template.ParseFiles("templates/rss.tmpl")
if err != nil {
c.HTML(http.StatusInternalServerError, "internal_server_error", &gin.H{})
return
}
c.Header("Content-Type", "text/xml")
err = t.Execute(c.Writer, posts)
if err != nil {
c.HTML(http.StatusInternalServerError, "internal_server_error", &gin.H{})
return
}
}

27
templates/rss.tmpl Normal file
View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>rawley.xyz</title>
<link>https://www.rawley.xyz</link>
<description>
Rawley's music and tech blog.
</description>
<language>en-us</language>
<image>
<title>rawley.xyz</title>
<link>https://www.rawley.xyz</link>
<url>https://rss.com/blog/wp-content/uploads/2019/10/social_style_3_rss-512-1.png</url>
<width>150</width>
<height>150</height>
</image>
</channel>
{{range $post := .}}
<item>
<title>{{$post.Title}}</title>
<pubDate>{{$post.Date}}</pubDate>
<link>https://rawley.xyz/blog/post/{{$val.Url}}</link>
<description></description>
<author>rawleyfowler@gmail.com</author>
</item>
{{end}}
</rss>