routes/routes.go (view raw)
1package routes
2
3import (
4 "html/template"
5 "log"
6 "net/http"
7 "path/filepath"
8
9 "github.com/alexedwards/flow"
10 gogit "github.com/go-git/go-git/v5"
11 "icyphox.sh/legit/config"
12 "icyphox.sh/legit/git"
13)
14
15type deps struct {
16 c *config.Config
17}
18
19func (d *deps) Repo(w http.ResponseWriter, r *http.Request) {
20 name := flow.Param(r.Context(), "name")
21 treePath := flow.Param(r.Context(), "...")
22
23 name = filepath.Clean(name)
24 // TODO: remove .git
25 path := filepath.Join(d.c.Git.ScanPath, name+".git")
26 repo, err := gogit.PlainOpen(path)
27 if err != nil {
28 Write404(w, *d.c)
29 return
30 }
31
32 files, err := git.FilesAtHead(repo, treePath)
33 if err != nil {
34 Write500(w, *d.c)
35 log.Println(err)
36 }
37
38 tpath := filepath.Join(d.c.Template.Dir, "*")
39 t := template.Must(template.ParseGlob(tpath))
40
41 data := make(map[string]interface{})
42 data["files"] = files
43 data["meta"] = d.c.Meta
44
45 if err := t.ExecuteTemplate(w, "repo", data); err != nil {
46 Write500(w, *d.c)
47 log.Println(err)
48 return
49 }
50}