From 2dd4cf35aab8324608a83d337459fd8354521b92 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Mon, 27 May 2024 22:36:50 +0200 Subject: feat: Wraps handler into its own package Although this creates more complex folder structure will allow in the feature for a easier testing of those given handlers. --- pkg/handler/about/handler.go | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 pkg/handler/about/handler.go (limited to 'pkg/handler/about/handler.go') diff --git a/pkg/handler/about/handler.go b/pkg/handler/about/handler.go new file mode 100644 index 0000000..a2caa4e --- /dev/null +++ b/pkg/handler/about/handler.go @@ -0,0 +1,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.HelloPage{ + Body: bs, + } + templates.WritePageTemplate(w, gitList) +} -- cgit v1.2.3