feat: RSS feeds for {news,guide}

feat/db
Francesco Minnocci 2 years ago
parent 9c10e1b8fc
commit c1a701353a
Signed by: BachoSeven
GPG Key ID: 2BE4AB7FDAD828A4

@ -4,6 +4,10 @@
{{define "body"}}
<section>
<h2>
Feed RSS
<a href="/guide/feed"> <i class="fa-solid fa-rss"></i></a>
</h2>
<h1>
<i class="fa-solid fa-person-chalkboard"></i>
Articoli

@ -4,6 +4,10 @@
{{define "body"}}
<section>
<h2>
Feed RSS
<a href="/news/feed"> <i class="fa-solid fa-rss"></i></a>
</h2>
<h1>
<i class="far fa-newspaper"></i>
Notizie Importanti

@ -17,6 +17,7 @@ require (
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/gorilla/feeds v1.1.1 // indirect
github.com/klauspost/compress v1.15.6 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.37.0 // indirect

@ -19,6 +19,8 @@ github.com/gofiber/fiber/v2 v2.34.0 h1:96BJMw6uaxQhJsHY54SFGOtGgp9pgombK5Hbi4JSE
github.com/gofiber/fiber/v2 v2.34.0/go.mod h1:ozRQfS+D7EL1+hMH+gutku0kfx1wLX4hAxDCtDzpj4U=
github.com/gofiber/redirect/v2 v2.1.23 h1:MqRyyeKyGqkF4GIFgTB4SuqIeeXviUglgRL2HCOFofM=
github.com/gofiber/redirect/v2 v2.1.23/go.mod h1:IYF5pPLDLYrrHMcxajDyWV+nHMbyPd6agCXkCnfLxS0=
github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY=
github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=

@ -9,6 +9,7 @@ import (
"git.phc.dm.unipi.it/phc/website/articles"
"git.phc.dm.unipi.it/phc/website/auth"
"git.phc.dm.unipi.it/phc/website/model"
"git.phc.dm.unipi.it/phc/website/rss"
"git.phc.dm.unipi.it/phc/website/templates"
"git.phc.dm.unipi.it/phc/website/util"
)
@ -27,6 +28,8 @@ type Service interface {
HandleProfilePage(w io.Writer, ctx Context) error
HandleNewsArticlePage(w io.Writer, articleID string, ctx Context) error
HandleGuideArticlePage(w io.Writer, articleID string, ctx Context) error
HandleNewsFeedPage(w io.Writer) error
HandleGuideFeedPage(w io.Writer) error
}
//
@ -188,3 +191,25 @@ func (h *DefaultHandler) HandleGuideArticlePage(w io.Writer, articleID string, c
"ContentHTML": template.HTML(html),
})
}
func (h *DefaultHandler) HandleNewsFeedPage(w io.Writer) error {
registry, err := h.NewsArticlesRegistry.GetArticles()
if err != nil {
return err
}
newsFeed := rss.GenerateRssFeed(registry)
return newsFeed.WriteRss(w)
}
func (h *DefaultHandler) HandleGuideFeedPage(w io.Writer) error {
registry, err := h.GuideArticlesRegistry.GetArticles()
if err != nil {
return err
}
guideFeed := rss.GenerateRssFeed(registry)
return guideFeed.WriteRss(w)
}

@ -0,0 +1,31 @@
package rss
import (
"git.phc.dm.unipi.it/phc/website/articles"
"github.com/gorilla/feeds"
)
func GenerateRssFeed(entries []*articles.Article) *feeds.Feed {
// Initialize RSS Feed
feed := &feeds.Feed{}
var feedItems []*feeds.Item
// Add items to RSS feeds
for _, entry := range entries {
feedItems = append(feedItems,
&feeds.Item{
Id: entry.Id,
Title: entry.Title,
Link: &feeds.Link{Href: entry.ArticlePath},
Description: entry.Description,
Created: entry.PublishDate,
})
}
feed.Items = feedItems
return feed
}

@ -105,6 +105,16 @@ func routes(h handler.Service, r fiber.Router) {
return h.HandleGuidePage(c, CreateContext(c))
})
r.Get("/news/feed", func(c *fiber.Ctx) error {
c.Type("xml")
return h.HandleNewsFeedPage(c)
})
r.Get("/guide/feed", func(c *fiber.Ctx) error {
c.Type("xml")
return h.HandleGuideFeedPage(c)
})
r.Post("/login", func(c *fiber.Ctx) error {
var loginForm struct {
Provider string `form:"provider"`

Loading…
Cancel
Save