aboutsummaryrefslogtreecommitdiff
path: root/backend/home.go
blob: 32634a108f6bb0644a4f96289c79ad4b1d061950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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) {
		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
	})
}