From f1e008670cd865520eb5f21fe6e7b56f02076a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Mon, 27 Oct 2025 13:32:39 +0100 Subject: feat(config): supports multiple sections --- backend/home.go | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) (limited to 'backend/home.go') diff --git a/backend/home.go b/backend/home.go index b945313..2f0643f 100644 --- a/backend/home.go +++ b/backend/home.go @@ -2,25 +2,21 @@ package backend import ( "html/template" - "log/slog" "net/http" "os" "path/filepath" - "strconv" "github.com/go-chi/chi/v5" ) var ( - sortedLogs []*logData + //sortedSections = map[string][]*sectionData{} rootContent = map[string]*rootData{} ) type homeData struct { *data - Logs []*logData - PagesNumber int - CurrentPage int + Sections []*Section } func (h *homeData) SetData(d *data) { @@ -29,7 +25,7 @@ func (h *homeData) SetData(d *data) { func HandleHome(r *chi.Mux) { r.Get("/", func(w http.ResponseWriter, r *http.Request) { - d := handleGenericLogsDisplay(w, r, 3) + d := handleGenericSectionDisplay(w, r, 3) if d == nil { return } @@ -93,29 +89,16 @@ func handleGenericRoot(w http.ResponseWriter, r *http.Request, name string) { d.handleGeneric(w, r, "simple", d) } -func handleGenericLogsDisplay(w http.ResponseWriter, r *http.Request, maxLogsPerPage int) *homeData { - rawPage := r.URL.Query().Get("page") - page := 1 - if rawPage != "" { - var err error - page, err = strconv.Atoi(rawPage) - if err != nil || page < 1 { - slog.Warn("invalid page number", "rawPage", rawPage) - w.WriteHeader(http.StatusBadRequest) - return nil - } - } +func handleGenericSectionDisplay(_ http.ResponseWriter, r *http.Request, maxLogsPerPage int) *homeData { d := new(homeData) d.data = new(data) - if sortedLogs == nil { - sortLogs() - } - d.CurrentPage = page - d.PagesNumber = max(1, (len(sortedLogs)-1)/maxLogsPerPage+1) - if d.PagesNumber < page { - notFound(w, r) - return nil + cfg := r.Context().Value(configKey).(*Config) + for _, sec := range cfg.Sections { + if len(sec.Data) == 0 { + sec.sort() + } + sec.Data = sec.Data[:min(maxLogsPerPage, len(sec.Data))] + d.Sections = append(d.Sections, &sec) } - d.Logs = sortedLogs[(page-1)*maxLogsPerPage : min(page*maxLogsPerPage, len(sortedLogs))] return d } -- cgit v1.2.3 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/home.go | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'backend/home.go') 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() } -- cgit v1.2.3