1package git
2
3import (
4 "log/slog"
5 "net/http"
6
7 "git.gabrielgio.me/cerrado/pkg/service"
8 "git.gabrielgio.me/cerrado/templates"
9 "github.com/go-git/go-git/v5/plumbing/object"
10 "github.com/gorilla/mux"
11)
12
13type (
14 GitHandler struct {
15 gitService gitService
16 }
17
18 gitService interface {
19 ListRepositories() ([]*service.Repository, error)
20 ListCommits(string) ([]*object.Commit, error)
21 }
22)
23
24func NewGitHandler(gitService gitService) *GitHandler {
25 return &GitHandler{
26 gitService: gitService,
27 }
28}
29
30func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) {
31 repos, err := g.gitService.ListRepositories()
32 if err != nil {
33 slog.Error("Error listing repo", "error", err)
34 return
35 }
36
37 gitList := &templates.GitListPage{repos}
38 templates.WritePageTemplate(w, gitList)
39}
40
41func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) {
42 name := mux.Vars(r)["name"]
43 gitList := &templates.GitItemPage{
44 Name: name,
45 GitItemBase: &templates.GitItemSummaryPage{},
46 }
47 templates.WritePageTemplate(w, gitList)
48}
49
50func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) {
51 name := mux.Vars(r)["name"]
52 gitList := &templates.GitItemPage{
53 Name: name,
54 GitItemBase: &templates.GitItemAboutPage{},
55 }
56 templates.WritePageTemplate(w, gitList)
57}
58
59func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) {
60 name := mux.Vars(r)["name"]
61 gitList := &templates.GitItemPage{
62 Name: name,
63 GitItemBase: &templates.GitItemRefsPage{},
64 }
65 templates.WritePageTemplate(w, gitList)
66}
67
68func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) {
69 name := mux.Vars(r)["name"]
70 gitList := &templates.GitItemPage{
71 Name: name,
72 GitItemBase: &templates.GitItemTreePage{},
73 }
74 templates.WritePageTemplate(w, gitList)
75}
76
77func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) {
78 name := mux.Vars(r)["name"]
79
80 commits, err := g.gitService.ListCommits(name)
81 if err != nil {
82 slog.Error("Error loading commits", "error", err)
83 return
84 }
85
86 gitList := &templates.GitItemPage{
87 Name: name,
88 GitItemBase: &templates.GitItemLogPage{
89 Commits: commits,
90 },
91 }
92 templates.WritePageTemplate(w, gitList)
93}