aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-06-07 14:02:25 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-06-07 14:02:25 +0200
commit18aa098f50e2a2c7db01dd4d04dde460fd40f5d5 (patch)
tree5e3b3d3ba3206f2765db1a23a378275852154a51 /pkg/handler
parentd0e0c1eb99303e1000140d4b98c610077278dc42 (diff)
downloadcerrado-18aa098f50e2a2c7db01dd4d04dde460fd40f5d5.tar.gz
cerrado-18aa098f50e2a2c7db01dd4d04dde460fd40f5d5.tar.bz2
cerrado-18aa098f50e2a2c7db01dd4d04dde460fd40f5d5.zip
feat: Add blob endpoint
Diffstat (limited to 'pkg/handler')
-rw-r--r--pkg/handler/git/handler.go81
-rw-r--r--pkg/handler/router.go3
2 files changed, 80 insertions, 4 deletions
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)