aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler/about.go
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-05-26 22:14:56 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-05-26 22:14:56 +0200
commit3d8637838e9ccfcb56899842945e760f337428b0 (patch)
tree2a2780e84b7ed00a0f56234f663fba92d44dfb53 /pkg/handler/about.go
parentadc207d2e6c39fa31283d83021e76300c0099c37 (diff)
downloadcerrado-3d8637838e9ccfcb56899842945e760f337428b0.tar.gz
cerrado-3d8637838e9ccfcb56899842945e760f337428b0.tar.bz2
cerrado-3d8637838e9ccfcb56899842945e760f337428b0.zip
feat: Add about page
Diffstat (limited to 'pkg/handler/about.go')
-rw-r--r--pkg/handler/about.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/pkg/handler/about.go b/pkg/handler/about.go
new file mode 100644
index 0000000..3ab2de8
--- /dev/null
+++ b/pkg/handler/about.go
@@ -0,0 +1,51 @@
+package handler
+
+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
+}
+
+func NewAboutHandler(readmePath string) *AboutHandler {
+ return &AboutHandler{readmePath}
+}
+
+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.HelloPage{
+ Body: bs,
+ }
+ templates.WritePageTemplate(w, gitList)
+}