aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler/git/handler.go
blob: 45cd865ea42c7929931a0472529d7916644bd8ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package git

import (
	"log/slog"
	"net/http"

	"git.gabrielgio.me/cerrado/pkg/service"
	"git.gabrielgio.me/cerrado/templates"
	"github.com/gorilla/mux"
)

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)
}

func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) {
	name := mux.Vars(r)["name"]
	gitList := &templates.GitItemPage{
		Name:        name,
		GitItemBase: &templates.GitItemSummaryPage{},
	}
	templates.WritePageTemplate(w, gitList)
}

func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) {
	name := mux.Vars(r)["name"]
	gitList := &templates.GitItemPage{
		Name:        name,
		GitItemBase: &templates.GitItemAboutPage{},
	}
	templates.WritePageTemplate(w, gitList)
}

func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) {
	name := mux.Vars(r)["name"]
	gitList := &templates.GitItemPage{
		Name:        name,
		GitItemBase: &templates.GitItemRefsPage{},
	}
	templates.WritePageTemplate(w, gitList)
}

func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) {
	name := mux.Vars(r)["name"]
	gitList := &templates.GitItemPage{
		Name:        name,
		GitItemBase: &templates.GitItemTreePage{},
	}
	templates.WritePageTemplate(w, gitList)
}

func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) {
	name := mux.Vars(r)["name"]
	gitList := &templates.GitItemPage{
		Name:        name,
		GitItemBase: &templates.GitItemLogPage{},
	}
	templates.WritePageTemplate(w, gitList)
}