From 6044dd841c81f0f4d206c6c8800b0fd30320cf13 Mon Sep 17 00:00:00 2001
From: Anhgelus Morhtuuzh
Date: Thu, 2 Oct 2025 22:35:58 +0200
Subject: feat(backend): display list of logs in home
---
backend/home.go | 36 +++++++++++++++++++++++++++++++++++-
backend/logs.go | 2 ++
backend/templates/home.html | 20 ++++++--------------
3 files changed, 43 insertions(+), 15 deletions(-)
(limited to 'backend')
diff --git a/backend/home.go b/backend/home.go
index 41f2fe5..32634a1 100644
--- a/backend/home.go
+++ b/backend/home.go
@@ -1,13 +1,47 @@
package backend
import (
+ "maps"
"net/http"
+ "slices"
+ "time"
"github.com/go-chi/chi/v5"
)
+var sortedLogs []*logData
+
+type homeData struct {
+ *data
+ Logs []*logData
+}
+
+func (h *homeData) SetData(d *data) {
+ h.data = d
+}
+
func HandleHome(r *chi.Mux) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
- new(data).handleGeneric(w, r, "home", nil)
+ d := new(homeData)
+ d.data = new(data)
+ if sortedLogs == nil {
+ sortLogs()
+ }
+ d.Logs = sortedLogs
+ d.handleGeneric(w, r, "home", d)
+ })
+}
+
+func sortLogs() {
+ slices.SortedFunc(maps.Values(logs), func(l *logData, l2 *logData) int {
+ lt := l.pubDate.AsTime(time.UTC)
+ l2t := l2.pubDate.AsTime(time.UTC)
+ // we want it reversed
+ if lt.Before(l2t) {
+ return 1
+ } else if lt.After(l2t) {
+ return -1
+ }
+ return 0
})
}
diff --git a/backend/logs.go b/backend/logs.go
index d15be41..ef44e0a 100644
--- a/backend/logs.go
+++ b/backend/logs.go
@@ -27,6 +27,7 @@ type logData struct {
Img image `toml:"image"`
pubDate toml.LocalDate `toml:"publication_date"`
Content template.HTML `toml:"-"`
+ Slug string `toml:"-"`
}
func (d *logData) SetData(dt *data) {
@@ -136,6 +137,7 @@ func parseLog(d *logData, path, slug string) bool {
d.Article = true
d.LogTitle = slug
d.title = slug
+ d.Slug = slug
b, err := os.ReadFile(path + ".md")
if err != nil {
if os.IsNotExist(err) {
diff --git a/backend/templates/home.html b/backend/templates/home.html
index fb927eb..f0b9b9c 100644
--- a/backend/templates/home.html
+++ b/backend/templates/home.html
@@ -6,26 +6,18 @@
+ {{ range .Logs }}
-
+
-
- A placeholder.
+
+ {{ .Img.Legend }}
- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim aeque doleamus animo, cum corpore dolemus, fieri tamen permagna accessio potest, si aliquod aeternum et infinitum impendere malum nobis opinemur.
-
-
-
-
-
-
- A placeholder.
-
-
- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim aeque doleamus animo, cum corpore dolemus, fieri tamen permagna accessio potest, si aliquod aeternum et infinitum impendere malum nobis opinemur.
+ {{ .Description }}
+ {{ end }}