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/config/config.go | 115 +++++++++++++++++++++++++++++++++++++++--- pkg/config/config_test.go | 14 ++--- pkg/git/git.go | 42 ++------------- 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 ----------------------- pkg/service/git.go | 26 ++++++---- 13 files changed, 347 insertions(+), 223 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') diff --git a/pkg/config/config.go b/pkg/config/config.go index 9b6acce..419d49d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,27 +1,126 @@ package config import ( + "errors" "fmt" "io" + "os" + "path" "strconv" "git.gabrielgio.me/cerrado/pkg/u" "git.sr.ht/~emersion/go-scfg" ) +var ( + ScanPathErr = errors.New("Scan path does not exist") + RepoPathErr = errors.New("Repository path does not exist") +) + type ( - Scan struct { + + // scan represents piece of the scan from the configuration file. + scan struct { Path string Public bool } - Configuration struct { - Scan *Scan + // configuration represents file configuration. + configuration struct { + Scan *scan RootReadme string } + + // This is a per repository configuration. + GitRepositoryConfiguration struct { + Name string + Path string + Public bool + } + + // ConfigurationRepository represents the configuration repository (as in + // database repositories). + // This holds all the function necessary to ask for configuration + // information. + ConfigurationRepository struct { + rootReadme string + repositories []*GitRepositoryConfiguration + } ) -func Parse(r io.Reader) (*Configuration, error) { +func LoadConfigurationRepository(configPath string) (*ConfigurationRepository, error) { + f, err := os.Open(configPath) + if err != nil { + return nil, err + } + + config, err := parse(f) + if err != nil { + return nil, err + } + + repo := &ConfigurationRepository{ + rootReadme: config.RootReadme, + } + + err = repo.expandOnScanPath(config.Scan.Path, config.Scan.Public) + if err != nil { + return nil, err + } + return repo, nil + +} + +// GetRootReadme returns root read path +func (c *ConfigurationRepository) GetRootReadme() string { + return c.rootReadme +} + +// GetByName returns configuration of repository for a given name. +// It returns nil if there is not match for it. +func (c *ConfigurationRepository) GetByName(name string) *GitRepositoryConfiguration { + for _, r := range c.repositories { + if r.Name == name { + return r + } + } + return nil +} + +// List returns all the configuration for all repositories. +func (c *ConfigurationRepository) List() []*GitRepositoryConfiguration { + return c.repositories +} + +// expandOnScanPath scans the scanPath for folders taking them as repositories +// and applying them default configuration. +func (c *ConfigurationRepository) expandOnScanPath(scanPath string, public bool) error { + if !u.FileExist(scanPath) { + return ScanPathErr + } + + entries, err := os.ReadDir(scanPath) + if err != nil { + return err + } + + c.repositories = make([]*GitRepositoryConfiguration, 0) + for _, e := range entries { + if !e.IsDir() { + continue + } + + fullPath := path.Join(scanPath, e.Name()) + c.repositories = append(c.repositories, &GitRepositoryConfiguration{ + Name: e.Name(), + Path: fullPath, + Public: public, + }) + } + return nil +} + +func parse(r io.Reader) (*configuration, error) { block, err := scfg.Read(r) if err != nil { return nil, err @@ -42,9 +141,9 @@ func Parse(r io.Reader) (*Configuration, error) { return config, nil } -func defaultConfiguration() *Configuration { - return &Configuration{ - Scan: &Scan{ +func defaultConfiguration() *configuration { + return &configuration{ + Scan: &scan{ Public: true, Path: "", }, @@ -57,7 +156,7 @@ func setRootReadme(block scfg.Block, readme *string) error { return setString(scanDir, readme) } -func setScan(block scfg.Block, scan *Scan) error { +func setScan(block scfg.Block, scan *scan) error { scanDir := block.Get("scan") err := setString(scanDir, &scan.Path) if err != nil { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index c8cd887..7afbaef 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -8,17 +8,17 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestConfig(t *testing.T) { +func TestFileParsing(t *testing.T) { testCases := []struct { name string config string - expectedConfig *Configuration + expectedConfig *configuration }{ { name: "minimal scan", config: `scan "/srv/git"`, - expectedConfig: &Configuration{ - Scan: &Scan{ + expectedConfig: &configuration{ + Scan: &scan{ Public: true, Path: "/srv/git", }, @@ -29,8 +29,8 @@ func TestConfig(t *testing.T) { config: `scan "/srv/git" { public false }`, - expectedConfig: &Configuration{ - Scan: &Scan{ + expectedConfig: &configuration{ + Scan: &scan{ Public: false, Path: "/srv/git", }, @@ -41,7 +41,7 @@ func TestConfig(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { r := strings.NewReader(tc.config) - config, err := Parse(r) + config, err := parse(r) if err != nil { t.Fatalf("Error parsing config %s", err.Error()) } diff --git a/pkg/git/git.go b/pkg/git/git.go index 85a3b95..b9ab235 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -2,63 +2,29 @@ package git import ( "errors" - "os" - "path" - "git.gabrielgio.me/cerrado/pkg/u" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" ) +var () + var ( - ScanPathErr = errors.New("Scan path does not exist") - RepoPathErr = errors.New("Repository path does not exist") - missingHeadErr = errors.New("Head not found") + MissingHeadErr = errors.New("Head not found") ) type ( - GitServerRepository struct { - scanPath string - } - GitRepository struct { path string } ) -func NewGitServerRepository(scanPath string) *GitServerRepository { - return &GitServerRepository{scanPath} -} - func NewGitRepository(dir string) *GitRepository { return &GitRepository{ path: dir, } } -func (g *GitServerRepository) List() ([]*GitRepository, error) { - if !u.FileExist(g.scanPath) { - return nil, ScanPathErr - } - - entries, err := os.ReadDir(g.scanPath) - if err != nil { - return nil, err - } - - repos := make([]*GitRepository, 0) - for _, e := range entries { - if !e.IsDir() { - continue - } - - fullPath := path.Join(g.scanPath, e.Name()) - repos = append(repos, NewGitRepository(fullPath)) - } - - return repos, nil -} - func (g *GitRepository) Path() string { return g.path } @@ -71,7 +37,7 @@ func (g *GitRepository) LastCommit() (*object.Commit, error) { ref, err := repo.Head() if err != nil { - return nil, errors.Join(missingHeadErr, err) + return nil, errors.Join(MissingHeadErr, err) } c, err := repo.CommitObject(ref.Hash()) 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) - } -} diff --git a/pkg/service/git.go b/pkg/service/git.go index 94ca75e..2b1fe25 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -3,44 +3,48 @@ package service import ( "path" + "git.gabrielgio.me/cerrado/pkg/config" "git.gabrielgio.me/cerrado/pkg/git" ) type ( - GitService struct { - server *git.GitServerRepository - } Repository struct { Name string Title string LastCommitMessage string LastCommitDate string } + + GitService struct { + configRepo configurationRepository + } + + configurationRepository interface { + List() []*config.GitRepositoryConfiguration + } ) // TODO: make it configurable const timeFormat = "2006.01.02 15:04:05" -func NewGitService(server *git.GitServerRepository) *GitService { +func NewGitService(configRepo configurationRepository) *GitService { return &GitService{ - server: server, + configRepo: configRepo, } } func (g *GitService) ListRepositories() ([]*Repository, error) { - rs, err := g.server.List() - if err != nil { - return nil, err - } + rs := g.configRepo.List() repos := make([]*Repository, len(rs)) for i, r := range rs { - obj, err := r.LastCommit() + repo := git.NewGitRepository(r.Path) + obj, err := repo.LastCommit() if err != nil { return nil, err } - baseName := path.Base(r.Path()) + baseName := path.Base(r.Path) repos[i] = &Repository{ Name: baseName, Title: baseName, -- cgit v1.2.3