aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorWilliam Hergès <william@herges.fr>2025-10-27 17:21:41 +0100
committerWilliam Hergès <william@herges.fr>2025-10-27 17:21:41 +0100
commit3ce41d99d688410a361d83767b50b64a35b569d4 (patch)
tree3e53a0a1356950575d152f3d8d1eb18bec33c633 /backend
parent0c3a89ee546a2a3215eb32d9786dabeda051812d (diff)
feat(sections): general rss feed
Diffstat (limited to 'backend')
-rw-r--r--backend/config.go1
-rw-r--r--backend/data.go14
-rw-r--r--backend/home.go40
-rw-r--r--backend/section.go4
-rw-r--r--backend/templates/base.html2
-rw-r--r--backend/templates/rss.xml10
6 files changed, 57 insertions, 14 deletions
diff --git a/backend/config.go b/backend/config.go
index 29e1397..cd24592 100644
--- a/backend/config.go
+++ b/backend/config.go
@@ -29,6 +29,7 @@ type Config struct {
Description string `toml:"description"`
DefaultImage string `toml:"default_image"`
Quotes []string `toml:"quotes"`
+ Language string `toml:"language"`
Sections []Section `toml:"section"`
diff --git a/backend/data.go b/backend/data.go
index 794b763..809e6e6 100644
--- a/backend/data.go
+++ b/backend/data.go
@@ -36,6 +36,7 @@ type data struct {
Links []Link
Logo *Logo
Quote string
+ Language string
}
func (d *data) SetData(data *data) {
@@ -74,6 +75,9 @@ func (d *data) merge(cfg *Config, r *http.Request) {
}
d.URL = r.URL.Path
}
+ if d.Language == "" {
+ d.Language = cfg.Language
+ }
}
func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string, custom dataUsable) {
@@ -117,7 +121,15 @@ func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string
func (d *data) handleRSS(w http.ResponseWriter, r *http.Request, custom dataUsable) {
cfg := r.Context().Value(configKey).(*Config)
d.merge(cfg, r)
- t, err := txt.New("").Funcs(txt.FuncMap{"first": templateFirst}).ParseFS(templates, "templates/rss.xml")
+ t, err := txt.New("").Funcs(txt.FuncMap{
+ "first": templateFirst,
+ "uri": func(s string) string {
+ if s == "" {
+ return ""
+ }
+ return s + "/"
+ },
+ }).ParseFS(templates, "templates/rss.xml")
if err != nil {
panic(err)
}
diff --git a/backend/home.go b/backend/home.go
index 2f0643f..8f274e8 100644
--- a/backend/home.go
+++ b/backend/home.go
@@ -2,15 +2,16 @@ package backend
import (
"html/template"
+ "iter"
"net/http"
"os"
"path/filepath"
+ "slices"
"github.com/go-chi/chi/v5"
)
var (
- //sortedSections = map[string][]*sectionData{}
rootContent = map[string]*rootData{}
)
@@ -25,7 +26,8 @@ func (h *homeData) SetData(d *data) {
func HandleHome(r *chi.Mux) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
- d := handleGenericSectionDisplay(w, r, 3)
+ cfg := r.Context().Value(configKey).(*Config)
+ d := handleGenericSectionDisplay(w, r, cfg.Sections, 3)
if d == nil {
return
}
@@ -58,6 +60,8 @@ func HandleRoot(r *chi.Mux, cfg *Config) {
if err != nil && !os.IsExist(err) {
panic(err)
}
+ r.Get("/rss", handleGenericRSS)
+ r.Get("/rss/", handleGenericRSS)
r.Get("/{name:[a-zA-Z-]+}", func(w http.ResponseWriter, r *http.Request) {
handleGenericRoot(w, r, chi.URLParam(r, "name"))
})
@@ -89,14 +93,40 @@ func handleGenericRoot(w http.ResponseWriter, r *http.Request, name string) {
d.handleGeneric(w, r, "simple", d)
}
-func handleGenericSectionDisplay(_ http.ResponseWriter, r *http.Request, maxLogsPerPage int) *homeData {
- d := new(homeData)
- d.data = new(data)
+func handleGenericRSS(w http.ResponseWriter, r *http.Request) {
cfg := r.Context().Value(configKey).(*Config)
+ var data iter.Seq[*sectionData]
for _, sec := range cfg.Sections {
if len(sec.Data) == 0 {
sec.sort()
}
+ var sl []*sectionData
+ for _, d := range sec.Data[:min(3, len(sec.Data))] {
+ dd := *d
+ dd.Slug = sec.URI + "/" + dd.Slug
+ sl = append(sl, &dd)
+ }
+ if data == nil {
+ data = slices.Values(sl)
+ } else {
+ data = slices.Values(slices.AppendSeq(sl, data))
+ }
+ }
+ var s Section
+ s.Data = sort(data)
+ s.Name = cfg.Name
+ s.Description = cfg.Description
+ s.URI = ""
+ s.handleRSS(w, r)
+}
+
+func handleGenericSectionDisplay(_ http.ResponseWriter, _ *http.Request, sections []Section, maxLogsPerPage int) *homeData {
+ d := new(homeData)
+ d.data = new(data)
+ for _, sec := range sections {
+ if len(sec.Data) == 0 {
+ sec.sort()
+ }
sec.Data = sec.Data[:min(maxLogsPerPage, len(sec.Data))]
d.Sections = append(d.Sections, &sec)
}
diff --git a/backend/section.go b/backend/section.go
index 06b48cc..a22d286 100644
--- a/backend/section.go
+++ b/backend/section.go
@@ -78,7 +78,7 @@ func (s *Section) Load(_ *Config) bool {
}
return false
}
- logger.Info("checking directory...", "path", s.Folder)
+ logger.Info("checking directory...")
err = s.readDir(s.Folder, dir)
if err != nil {
slog.Error("reading directory", "error", err)
@@ -168,7 +168,7 @@ func (s *Section) handleList(w http.ResponseWriter, r *http.Request) {
}
func (s *Section) handleRSS(w http.ResponseWriter, r *http.Request) {
- d := handleGenericSectionDisplay(w, r, 5)
+ d := handleGenericSectionDisplay(w, r, []Section{*s}, 5)
if d == nil {
return
}
diff --git a/backend/templates/base.html b/backend/templates/base.html
index 0a54096..436a1c2 100644
--- a/backend/templates/base.html
+++ b/backend/templates/base.html
@@ -1,5 +1,5 @@
<!doctype html>
-<html lang="fr" prefix="og: https://ogp.me/ns/article#">
+<html lang="{{ .Language }}" prefix="og: https://ogp.me/ns/article#">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
diff --git a/backend/templates/rss.xml b/backend/templates/rss.xml
index 2abca8e..039a2f7 100644
--- a/backend/templates/rss.xml
+++ b/backend/templates/rss.xml
@@ -4,16 +4,16 @@
<atom:link href="https://{{ .Domain }}{{ .URL }}" rel="self" type="application/rss+xml" />
<title>{{ .Title }}</title>
{{ $sec := first .Sections }}
- {{ $uri := $sec.URI }}
- <link>https://{{ .Domain }}/{{ $uri }}/</link>
+ {{ $uri := uri $sec.URI }}
+ <link>https://{{ .Domain }}/{{ $uri }}</link>
<description>{{ .PageDescription }}</description>
- <language>fr-fr</language>
+ <language>{{ .Language }}</language>
{{ $domain := .Domain }}
{{ range $sec.Data }}
<item>
<title>{{ .DataTitle }}</title>
- <link>https://{{ $domain }}/{{ $uri }}/{{ .Slug }}</link>
- <guid>https://{{ $domain }}/{{ $uri }}/{{ .Slug }}</guid>
+ <link>https://{{ $domain }}/{{ $uri }}{{ .Slug }}</link>
+ <guid>https://{{ $domain }}/{{ $uri }}{{ .Slug }}</guid>
<description>{{ .Description }}</description>
<pubDate>{{ .PubDateRSS }}</pubDate>
</item>