From 3ce41d99d688410a361d83767b50b64a35b569d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Mon, 27 Oct 2025 17:21:41 +0100 Subject: feat(sections): general rss feed --- backend/config.go | 1 + backend/data.go | 14 +++++++++++++- backend/home.go | 40 +++++++++++++++++++++++++++++++++++----- backend/section.go | 4 ++-- backend/templates/base.html | 2 +- backend/templates/rss.xml | 10 +++++----- 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,11 +93,37 @@ 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() } 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 @@ - + 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 @@ {{ .Title }} {{ $sec := first .Sections }} - {{ $uri := $sec.URI }} - https://{{ .Domain }}/{{ $uri }}/ + {{ $uri := uri $sec.URI }} + https://{{ .Domain }}/{{ $uri }} {{ .PageDescription }} - fr-fr + {{ .Language }} {{ $domain := .Domain }} {{ range $sec.Data }} {{ .DataTitle }} - https://{{ $domain }}/{{ $uri }}/{{ .Slug }} - https://{{ $domain }}/{{ $uri }}/{{ .Slug }} + https://{{ $domain }}/{{ $uri }}{{ .Slug }} + https://{{ $domain }}/{{ $uri }}{{ .Slug }} {{ .Description }} {{ .PubDateRSS }} -- cgit v1.2.3