finish up the atom news feed API

This commit is contained in:
ngn
2025-01-04 19:59:44 +03:00
parent 26e8909998
commit 337e56de78
33 changed files with 2633 additions and 2633 deletions

View File

@ -16,7 +16,7 @@ func (ml *Multilang) Supports(lang string) bool {
ml_ref := reflect.ValueOf(ml).Elem()
for i := 0; i < reflect.Indirect(ml_ref).NumField(); i++ {
if name := reflect.Indirect(ml_ref).Field(i).Type().Name(); strings.ToLower(name) == lang {
if name := reflect.Indirect(ml_ref).Type().Field(i).Name; strings.ToLower(name) == lang {
return true
}
}

View File

@ -57,7 +57,7 @@ func (n *News) Scan(rows *sql.Rows) (err error) {
}
func (n *News) IsValid() bool {
return n.Author != "" && n.ID != "" && !n.Title.Empty() && !n.Content.Empty()
return n.Time != 0 && n.Author != "" && n.ID != "" && !n.Title.Empty() && !n.Content.Empty()
}
func (db *Type) NewsNext(n *News) bool {

View File

@ -2,7 +2,11 @@ module github.com/ngn13/website/api
go 1.21.3
require github.com/gofiber/fiber/v2 v2.52.5
require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/mattn/go-sqlite3 v1.14.24
github.com/russross/blackfriday/v2 v2.1.0
)
require (
github.com/andybalholm/brotli v1.0.5 // indirect
@ -11,9 +15,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect

View File

@ -138,6 +138,8 @@ func PUT_AddNews(c *fiber.Ctx) error {
return util.ErrBadJSON(c)
}
news.Time = uint64(time.Now().Unix())
if !news.IsValid() {
return util.ErrBadReq(c)
}

View File

@ -1,7 +1,9 @@
package routes
import (
"sort"
"strings"
"time"
"github.com/gofiber/fiber/v2"
"github.com/ngn13/website/api/config"
@ -9,12 +11,31 @@ import (
"github.com/ngn13/website/api/util"
)
// feed_entry is a temporary struct used to pass the news to the news.xml
type feed_entry struct {
Title string
Author string
Time time.Time
RFC3339 string
Content string
}
// convert UNIX timestamp to RFC3339 (format used by atom feeds)
func (e *feed_entry) From(news *database.News, lang string) {
e.Title = news.Title.Get(lang)
e.Author = news.Author
e.Time = time.Unix(int64(news.Time), 0)
e.RFC3339 = e.Time.Format(time.RFC3339)
e.Content = news.Content.Get(lang)
}
func GET_News(c *fiber.Ctx) error {
var (
news []database.News
n database.News
feed []byte
err error
entries []feed_entry
news database.News
indx uint64
feed []byte
err error
)
db := c.Locals("database").(*database.Type)
@ -27,17 +48,25 @@ func GET_News(c *fiber.Ctx) error {
}
lang = strings.ToLower(lang)
indx = 0
for db.NewsNext(&n) {
if n.Supports(lang) {
news = append(news, n)
for db.NewsNext(&news) {
if news.Supports(lang) {
entries = append(entries, feed_entry{})
entries[indx].From(&news, lang)
indx++
}
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].Time.Before(entries[j].Time)
})
if feed, err = util.Render("views/news.xml", fiber.Map{
"frontend": frontend,
"updated": time.Now().Format(time.RFC3339),
"entries": entries,
"lang": lang,
"news": news,
}); err != nil {
return util.ErrInternal(c, err)
}

View File

@ -1,6 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom">
<title>{{.frontend.Host}} news</title>
<updated>2025-01-02T20:46:24Z</updated>
<subtitle>News and updates about my self-hosted services and projects</subtitle>
<link href="{{.frontend.String}}/news"></link>
<updated>{{.updated}}</updated>
<subtitle>News and updates about my projects and self-hosted services</subtitle>
<link href="{{.frontend.JoinPath "/news"}}"></link>
{{ range .entries }}
<entry>
<title>{{.Title}}</title>
<updated>{{.RFC3339}}</updated>
<author>
<name>{{.Author}}</name>
</author>
<content>{{.Content}}</content>
</entry>
{{ end }}
</feed>