aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler/about/handler.go
blob: 1acde601c7aa036550ab6e73253f7c820646e1e6 (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
48
49
50
51
52
53
54
55
56
57
package about

import (
	"io"
	"log/slog"
	"net/http"
	"os"

	"github.com/gomarkdown/markdown"
	"github.com/gomarkdown/markdown/html"
	"github.com/gomarkdown/markdown/parser"

	"git.gabrielgio.me/cerrado/templates"
)

type (
	AboutHandler struct {
		readmePath string
	}

	configurationRepository interface {
		GetRootReadme() string
	}
)

func NewAboutHandler(configRepo configurationRepository) *AboutHandler {
	return &AboutHandler{configRepo.GetRootReadme()}
}

func (g *AboutHandler) About(w http.ResponseWriter, _ *http.Request) {
	f, err := os.Open(g.readmePath)
	if err != nil {
		slog.Error("Error loading readme file", "error", err)
		return
	}

	bs, err := io.ReadAll(f)
	if err != nil {
		slog.Error("Error reading readme file bytes", "error", err)
		return
	}

	extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
	p := parser.NewWithExtensions(extensions)
	doc := p.Parse(bs)

	htmlFlags := html.CommonFlags | html.HrefTargetBlank
	opts := html.RendererOptions{Flags: htmlFlags}
	renderer := html.NewRenderer(opts)

	bs = markdown.Render(doc, renderer)

	gitList := &templates.AboutPage{
		Body: bs,
	}
	templates.WritePageTemplate(w, gitList)
}