all repos — legit @ ce71721c6dc80db8af63f2098a1548308e2621b2

web frontend for git

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	"icyphox.sh/legit/config"
 11	"icyphox.sh/legit/git"
 12)
 13
 14type deps struct {
 15	c *config.Config
 16}
 17
 18func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
 19	name := flow.Param(r.Context(), "name")
 20	name = filepath.Clean(name)
 21	// TODO: remove .git
 22	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 23	gr, err := git.Open(path, "")
 24	if err != nil {
 25		Write404(w, *d.c)
 26		return
 27	}
 28
 29	files, err := gr.FileTree("")
 30	if err != nil {
 31		Write500(w, *d.c)
 32		log.Println(err)
 33		return
 34	}
 35
 36	data := make(map[string]any)
 37	data["name"] = name
 38	// TODO: make this configurable
 39	data["ref"] = "master"
 40
 41	d.listFiles(files, data, w)
 42	return
 43}
 44
 45func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
 46	name := flow.Param(r.Context(), "name")
 47	treePath := flow.Param(r.Context(), "...")
 48	ref := flow.Param(r.Context(), "ref")
 49
 50	name = filepath.Clean(name)
 51	// TODO: remove .git
 52	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 53	gr, err := git.Open(path, ref)
 54	if err != nil {
 55		Write404(w, *d.c)
 56		return
 57	}
 58
 59	files, err := gr.FileTree(treePath)
 60	if err != nil {
 61		Write500(w, *d.c)
 62		log.Println(err)
 63		return
 64	}
 65
 66	data := make(map[string]any)
 67	data["name"] = name
 68	data["ref"] = ref
 69	data["parent"] = treePath
 70
 71	d.listFiles(files, data, w)
 72	return
 73}
 74
 75func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
 76	name := flow.Param(r.Context(), "name")
 77	treePath := flow.Param(r.Context(), "...")
 78	ref := flow.Param(r.Context(), "ref")
 79
 80	name = filepath.Clean(name)
 81	// TODO: remove .git
 82	path := filepath.Join(d.c.Git.ScanPath, name+".git")
 83	gr, err := git.Open(path, ref)
 84	if err != nil {
 85		Write404(w, *d.c)
 86		return
 87	}
 88
 89	contents, err := gr.FileContent(treePath)
 90	data := make(map[string]any)
 91	data["name"] = name
 92	data["ref"] = ref
 93
 94	d.showFile(contents, data, w)
 95	return
 96}
 97
 98func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
 99	name := flow.Param(r.Context(), "name")
100	ref := flow.Param(r.Context(), "ref")
101
102	path := filepath.Join(d.c.Git.ScanPath, name+".git")
103	gr, err := git.Open(path, ref)
104	if err != nil {
105		Write404(w, *d.c)
106		return
107	}
108
109	commits, err := gr.Commits()
110	if err != nil {
111		Write500(w, *d.c)
112		log.Println(err)
113		return
114	}
115
116	tpath := filepath.Join(d.c.Template.Dir, "*")
117	t := template.Must(template.ParseGlob(tpath))
118
119	data := make(map[string]interface{})
120	data["commits"] = commits
121	data["meta"] = d.c.Meta
122	data["name"] = name
123	data["ref"] = ref
124
125	if err := t.ExecuteTemplate(w, "log", data); err != nil {
126		log.Println(err)
127		return
128	}
129}