diff options
Diffstat (limited to 'backend/home.go')
| -rw-r--r-- | backend/home.go | 40 |
1 files changed, 35 insertions, 5 deletions
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) } |
