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.go | 51 --------------------------------- pkg/handler/about/handler.go | 57 +++++++++++++++++++++++++++++++++++++ pkg/handler/config/handler.go | 65 +++++++++++++++++++++++++++++++++++++++++++ pkg/handler/git.go | 28 ------------------- pkg/handler/git/handler.go | 36 ++++++++++++++++++++++++ pkg/handler/router.go | 38 +++++++++++++++++++++++++ pkg/handler/static.go | 18 ------------ pkg/handler/static/handler.go | 18 ++++++++++++ pkg/handler/status.go | 62 ----------------------------------------- 9 files changed, 214 insertions(+), 159 deletions(-) delete mode 100644 pkg/handler/about.go create mode 100644 pkg/handler/about/handler.go create mode 100644 pkg/handler/config/handler.go delete mode 100644 pkg/handler/git.go create mode 100644 pkg/handler/git/handler.go create mode 100644 pkg/handler/router.go delete mode 100644 pkg/handler/static.go create mode 100644 pkg/handler/static/handler.go delete mode 100644 pkg/handler/status.go (limited to 'pkg/handler') diff --git a/pkg/handler/about.go b/pkg/handler/about.go deleted file mode 100644 index 3ab2de8..0000000 --- a/pkg/handler/about.go +++ /dev/null @@ -1,51 +0,0 @@ -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) -} 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) +} diff --git a/pkg/handler/config/handler.go b/pkg/handler/config/handler.go new file mode 100644 index 0000000..c278e35 --- /dev/null +++ b/pkg/handler/config/handler.go @@ -0,0 +1,65 @@ +package config + +import ( + "bytes" + "encoding/json" + "log/slog" + "net/http" + + "github.com/alecthomas/chroma/v2/formatters/html" + "github.com/alecthomas/chroma/v2/lexers" + "github.com/alecthomas/chroma/v2/styles" + + "git.gabrielgio.me/cerrado/pkg/config" + "git.gabrielgio.me/cerrado/templates" +) + +type ( + configurationRepository interface { + GetRootReadme() string + List() []*config.GitRepositoryConfiguration + } +) + +func ConfigFile(configRepo configurationRepository) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, _ *http.Request) { + + config := struct { + RootReadme string + Repositories []*config.GitRepositoryConfiguration + }{ + RootReadme: configRepo.GetRootReadme(), + Repositories: configRepo.List(), + } + + b, err := json.MarshalIndent(config, "", " ") + if err != nil { + slog.Error("Error parsing json", "error", err) + return + } + + lexer := lexers.Get("json") + style := styles.Get("monokailight") + formatter := html.New( + html.WithLineNumbers(true), + ) + iterator, err := lexer.Tokenise(nil, string(b)) + if err != nil { + slog.Error("Error tokenise", "error", err) + return + } + + var code bytes.Buffer + err = formatter.Format(&code, style, iterator) + if err != nil { + slog.Error("Error format", "error", err) + return + } + + hello := &templates.HelloPage{ + Body: code.Bytes(), + } + + templates.WritePageTemplate(w, hello) + } +} diff --git a/pkg/handler/git.go b/pkg/handler/git.go deleted file mode 100644 index 1ed2c49..0000000 --- a/pkg/handler/git.go +++ /dev/null @@ -1,28 +0,0 @@ -package handler - -import ( - "log/slog" - "net/http" - - "git.gabrielgio.me/cerrado/pkg/service" - "git.gabrielgio.me/cerrado/templates" -) - -type GitHandler struct { - gitService *service.GitService -} - -func NewGitHandler(gitService *service.GitService) *GitHandler { - return &GitHandler{gitService} -} - -func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { - repos, err := g.gitService.ListRepositories() - if err != nil { - slog.Error("Error listing repo", "error", err) - return - } - - gitList := &templates.GitListPage{repos} - templates.WritePageTemplate(w, gitList) -} diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go new file mode 100644 index 0000000..236ac41 --- /dev/null +++ b/pkg/handler/git/handler.go @@ -0,0 +1,36 @@ +package git + +import ( + "log/slog" + "net/http" + + "git.gabrielgio.me/cerrado/pkg/service" + "git.gabrielgio.me/cerrado/templates" +) + +type ( + GitHandler struct { + gitService gitService + } + + gitService interface { + ListRepositories() ([]*service.Repository, error) + } +) + +func NewGitHandler(gitService gitService) *GitHandler { + return &GitHandler{ + gitService: gitService, + } +} + +func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { + repos, err := g.gitService.ListRepositories() + if err != nil { + slog.Error("Error listing repo", "error", err) + return + } + + gitList := &templates.GitListPage{repos} + templates.WritePageTemplate(w, gitList) +} diff --git a/pkg/handler/router.go b/pkg/handler/router.go new file mode 100644 index 0000000..a8c9c6f --- /dev/null +++ b/pkg/handler/router.go @@ -0,0 +1,38 @@ +package handler + +import ( + "net/http" + + serverconfig "git.gabrielgio.me/cerrado/pkg/config" + "git.gabrielgio.me/cerrado/pkg/handler/about" + "git.gabrielgio.me/cerrado/pkg/handler/config" + "git.gabrielgio.me/cerrado/pkg/handler/git" + "git.gabrielgio.me/cerrado/pkg/handler/static" + "git.gabrielgio.me/cerrado/pkg/service" +) + +// Mount handler gets the requires service and repository to build the handlers +// This functons wraps the whole handler package and wraps it into one part so +// its sub package don't leak in other places. +func MountHandler( + gitService *service.GitService, + configRepo *serverconfig.ConfigurationRepository, +) (http.Handler, error) { + var ( + gitHandler = git.NewGitHandler(gitService) + aboutHandler = about.NewAboutHandler(configRepo) + configHander = config.ConfigFile(configRepo) + ) + + staticHandler, err := static.NewStaticHander("/static/") + if err != nil { + return nil, err + } + + mux := http.NewServeMux() + mux.Handle("/static/", staticHandler) + mux.HandleFunc("/config", configHander) + mux.HandleFunc("/about", aboutHandler.About) + mux.HandleFunc("/", gitHandler.List) + return mux, nil +} diff --git a/pkg/handler/static.go b/pkg/handler/static.go deleted file mode 100644 index 9f312f4..0000000 --- a/pkg/handler/static.go +++ /dev/null @@ -1,18 +0,0 @@ -package handler - -import ( - "io/fs" - "net/http" - - "git.gabrielgio.me/cerrado/static" -) - -func NewStaticHander(prefix string) (http.Handler, error) { - staticFs, err := fs.Sub(static.Static, ".") - if err != nil { - return nil, err - } - - handler := http.StripPrefix(prefix, http.FileServer(http.FS(staticFs))) - return handler, nil -} diff --git a/pkg/handler/static/handler.go b/pkg/handler/static/handler.go new file mode 100644 index 0000000..6a826cc --- /dev/null +++ b/pkg/handler/static/handler.go @@ -0,0 +1,18 @@ +package static + +import ( + "io/fs" + "net/http" + + "git.gabrielgio.me/cerrado/static" +) + +func NewStaticHander(prefix string) (http.Handler, error) { + staticFs, err := fs.Sub(static.Static, ".") + if err != nil { + return nil, err + } + + handler := http.StripPrefix(prefix, http.FileServer(http.FS(staticFs))) + return handler, nil +} diff --git a/pkg/handler/status.go b/pkg/handler/status.go deleted file mode 100644 index 9baac2c..0000000 --- a/pkg/handler/status.go +++ /dev/null @@ -1,62 +0,0 @@ -package handler - -import ( - "bytes" - "encoding/json" - "log/slog" - "net/http" - "os" - - "github.com/alecthomas/chroma/v2/formatters/html" - "github.com/alecthomas/chroma/v2/lexers" - "github.com/alecthomas/chroma/v2/styles" - - "git.gabrielgio.me/cerrado/pkg/config" - "git.gabrielgio.me/cerrado/templates" -) - -func ConfigFile(configPath string) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, _ *http.Request) { - f, err := os.Open(configPath) - if err != nil { - slog.Error("Error openning config file", "error", err, "path", configPath) - return - } - - c, err := config.Parse(f) - if err != nil { - slog.Error("Error parsing config", "error", err, "path", configPath) - return - } - - b, err := json.MarshalIndent(c, "", " ") - if err != nil { - slog.Error("Error parsing json", "error", err) - return - } - - lexer := lexers.Get("json") - style := styles.Get("monokailight") - formatter := html.New( - html.WithLineNumbers(true), - ) - iterator, err := lexer.Tokenise(nil, string(b)) - if err != nil { - slog.Error("Error tokenise", "error", err) - return - } - - var code bytes.Buffer - err = formatter.Format(&code, style, iterator) - if err != nil { - slog.Error("Error format", "error", err) - return - } - - hello := &templates.HelloPage{ - Body: code.Bytes(), - } - - templates.WritePageTemplate(w, hello) - } -} -- cgit v1.2.3