aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-10-02 22:35:58 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2025-10-02 22:35:58 +0200
commit6044dd841c81f0f4d206c6c8800b0fd30320cf13 (patch)
tree7498d483e661e0c87d3af2f7ab03eacda48fe30b
parent5dfd71bfe1c0e25da413f00256a302b13c88d26d (diff)
feat(backend): display list of logs in home
-rw-r--r--backend/home.go36
-rw-r--r--backend/logs.go2
-rw-r--r--backend/templates/home.html20
-rw-r--r--go.sum25
4 files changed, 68 insertions, 15 deletions
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 @@
</p>
</div>
<article>
+ {{ range .Logs }}
<article>
- <h2><a href="log.html">Article title</a></h2>
+ <h2><a href="/log/{{ .Slug }}">{{ .LogTitle }}</a></h2>
<figure>
- <a href="log.html"><img src="https://placehold.co/1920x1080" alt=""></a>
- <figcaption>A placeholder.</figcaption>
+ <a href="/log/{{ .Slug }}"><img src="{{ .Img.Src }}" alt="{{ .Img.Alt }}"></a>
+ <figcaption>{{ .Img.Legend }}</figcaption>
</figure>
<p>
- 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.
- </p>
- </article>
- <article>
- <h2><a href="log.html">Article title</a></h2>
- <figure>
- <a href="log.html"><img src="https://placehold.co/1920x1080" alt=""></a>
- <figcaption>A placeholder.</figcaption>
- </figure>
- <p>
- 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 }}
</p>
</article>
+ {{ end }}
<div class="pagination">
<a href="">Précédent</a>
<p>01/15</p>
diff --git a/go.sum b/go.sum
index e8af854..a234c8e 100644
--- a/go.sum
+++ b/go.sum
@@ -1,8 +1,33 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/go-chi/httplog/v3 v3.2.2 h1:G0oYv3YYcikNjijArHFUlqfR78cQNh9fGT43i6StqVc=
github.com/go-chi/httplog/v3 v3.2.2/go.mod h1:N/J1l5l1fozUrqIVuT8Z/HzNeSy8TF2EFyokPLe6y2w=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
+github.com/pelletier/go-toml/v2 v2.0.0 h1:P7Bq0SaI8nsexyay5UAyDo+ICWy5MQPgEZ5+l8JQTKo=
+github.com/pelletier/go-toml/v2 v2.0.0/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
+github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=
+github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
+github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw=
+github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI=
+github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
+github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
+github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeBAsoHo=
+github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
+github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
+github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=