From 18aa098f50e2a2c7db01dd4d04dde460fd40f5d5 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Fri, 7 Jun 2024 14:02:25 +0200 Subject: feat: Add blob endpoint --- pkg/handler/git/handler.go | 81 ++++++++++++++++++++++++++++++++++++++++++++-- pkg/handler/router.go | 3 +- 2 files changed, 80 insertions(+), 4 deletions(-) (limited to 'pkg/handler') diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index e2f4042..f3e74c7 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -1,11 +1,17 @@ package git import ( + "bytes" "log/slog" "net/http" + "path/filepath" "git.gabrielgio.me/cerrado/pkg/service" "git.gabrielgio.me/cerrado/templates" + "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/formatters/html" + "github.com/alecthomas/chroma/v2/lexers" + "github.com/alecthomas/chroma/v2/styles" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) @@ -19,6 +25,8 @@ type ( ListRepositories() ([]*service.Repository, error) ListCommits(name string, ref string) ([]*object.Commit, error) GetHead(name string) (*plumbing.Reference, error) + GetTree(name, ref, path string) (*object.Tree, error) + GetFileContent(name, ref, path string) (string, error) ListTags(name string) ([]*object.Tag, error) ListBranches(name string) ([]*plumbing.Reference, error) } @@ -107,10 +115,64 @@ func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { name := r.PathValue("name") ref := r.PathValue("ref") + rest := r.PathValue("rest") + + tree, err := g.gitService.GetTree(name, ref, rest) + if err != nil { + slog.Error("Error loading tree", "error", err) + return + } + gitList := &templates.GitItemPage{ - Name: name, - Ref: ref, - GitItemBase: &templates.GitItemTreePage{}, + Name: name, + Ref: ref, + GitItemBase: &templates.GitItemTreePage{ + CurrentPath: rest, + Tree: tree, + Ref: ref, + Name: name, + }, + } + templates.WritePageTemplate(w, gitList) +} + +func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { + name := r.PathValue("name") + ref := r.PathValue("ref") + rest := r.PathValue("rest") + + file, err := g.gitService.GetFileContent(name, ref, rest) + if err != nil { + slog.Error("Error loading blob", "error", err) + return + } + + filename := filepath.Base(rest) + lexer := GetLexers(filename) + style := styles.Get("xcode") + formatter := html.New( + html.WithLineNumbers(true), + ) + iterator, err := lexer.Tokenise(nil, file) + 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 + } + + gitList := &templates.GitItemPage{ + Name: name, + Ref: ref, + GitItemBase: &templates.GitItemBlobPage{ + File: rest, + Content: code.Bytes(), + }, } templates.WritePageTemplate(w, gitList) } @@ -134,3 +196,16 @@ func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) { } templates.WritePageTemplate(w, gitList) } + +func GetLexers(filename string) chroma.Lexer { + if filename == "APKBUILD" { + return lexers.Get("sh") + } + + lexer := lexers.Get(filename) + + if lexer == nil { + lexer = lexers.Get("txt") + } + return lexer +} diff --git a/pkg/handler/router.go b/pkg/handler/router.go index bdf883e..ed782f7 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -35,7 +35,8 @@ func MountHandler( mux.HandleFunc("/{name}/about/{$}", gitHandler.About) mux.HandleFunc("/{name}", gitHandler.Summary) mux.HandleFunc("/{name}/refs/{$}", gitHandler.Refs) - mux.HandleFunc("/{name}/tree/{ref}", gitHandler.Tree) + mux.HandleFunc("/{name}/tree/{ref}/{rest...}", gitHandler.Tree) + mux.HandleFunc("/{name}/blob/{ref}/{rest...}", gitHandler.Blob) mux.HandleFunc("/{name}/log/{ref}", gitHandler.Log) mux.HandleFunc("/config", configHander) mux.HandleFunc("/about", aboutHandler.About) -- cgit v1.2.3