ui.go (view raw)
1package main
2
3import (
4 "embed"
5 "fmt"
6 "html/template"
7 "log"
8
9 "github.com/gin-gonic/gin"
10)
11
12func padZero(i int) string {
13 return fmt.Sprintf("%02d", i)
14}
15
16var (
17 //go:embed templates/index.html
18 templates embed.FS
19 indexTemplate *template.Template
20 funcMap = template.FuncMap{"padZero": padZero}
21)
22
23func ParseTemplates() {
24 var err error
25 indexTemplate, err = template.New("index.html").Funcs(funcMap).ParseFS(templates, "templates/index.html")
26 if err != nil {
27 log.Fatal("Could not parse index template")
28 return
29 }
30}
31
32func ShowIndexPage(c *gin.Context) {
33 var occurrences []Occurrence
34 db.Order("month, day").Find(&occurrences)
35
36 data := struct {
37 Occurrences []Occurrence
38 }{
39 Occurrences: occurrences,
40 }
41
42 if indexTemplate.Execute(c.Writer, data) != nil {
43 c.String(500, "Internal Server Error")
44 }
45}