all repos — legit @ 1e7b63814fc003577e14356c79793cc827661270

web frontend for git, written in go

routes/routes.go (view raw)

  1package routes
  2
  3import (
  4	"fmt"
  5	"html/template"
  6	"log"
  7	"net/http"
  8	"os"
  9	"path/filepath"
 10	"sort"
 11	"time"
 12
 13	"git.icyphox.sh/legit/config"
 14	"git.icyphox.sh/legit/git"
 15	"github.com/alexedwards/flow"
 16	"github.com/dustin/go-humanize"
 17	"github.com/microcosm-cc/bluemonday"
 18	"github.com/russross/blackfriday/v2"
 19)
 20
 21type deps struct {
 22	c *config.Config
 23}
 24
 25func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
 26	dirs, err := os.ReadDir(d.c.Repo.ScanPath)
 27	if err != nil {
 28		d.Write500(w)
 29		log.Printf("reading scan path: %s", err)
 30		return
 31	}
 32
 33	type info struct {
 34		Name, Desc, Idle string
 35		d                time.Time
 36	}
 37
 38	infos := []info{}
 39
 40	for _, dir := range dirs {
 41		if d.isIgnored(dir.Name()) {
 42			continue
 43		}
 44
 45		path := filepath.Join(d.c.Repo.ScanPath, dir.Name())
 46		gr, err := git.Open(path, "")
 47		if err != nil {
 48			continue
 49		}
 50
 51		c, err := gr.LastCommit()
 52		if err != nil {
 53			d.Write500(w)
 54			log.Println(err)
 55			return
 56		}
 57
 58		desc := getDescription(path)
 59
 60		infos = append(infos, info{
 61			Name: dir.Name(),
 62			Desc: desc,
 63			Idle: humanize.Time(c.Author.When),
 64			d:    c.Author.When,
 65		})
 66	}
 67
 68	sort.Slice(infos, func(i, j int) bool {
 69		return infos[j].d.Before(infos[i].d)
 70	})
 71
 72	tpath := filepath.Join(d.c.Dirs.Templates, "*")
 73	t := template.Must(template.ParseGlob(tpath))
 74
 75	data := make(map[string]interface{})
 76	data["meta"] = d.c.Meta
 77	data["info"] = infos
 78
 79	if err := t.ExecuteTemplate(w, "index", data); err != nil {
 80		log.Println(err)
 81		return
 82	}
 83}
 84
 85func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
 86	name := flow.Param(r.Context(), "name")
 87	if d.isIgnored(name) {
 88		d.Write404(w)
 89		return
 90	}
 91	name = filepath.Clean(name)
 92	path := filepath.Join(d.c.Repo.ScanPath, name)
 93
 94	gr, err := git.Open(path, "")
 95	if err != nil {
 96		d.Write404(w)
 97		return
 98	}
 99
100	commits, err := gr.Commits()
101	if err != nil {
102		d.Write500(w)
103		log.Println(err)
104		return
105	}
106
107	var readmeContent template.HTML
108	for _, readme := range d.c.Repo.Readme {
109		ext := filepath.Ext(readme)
110		content, _ := gr.FileContent(readme)
111		if len(content) > 0 {
112			switch ext {
113			case ".md", ".mkd", ".markdown":
114				unsafe := blackfriday.Run(
115					[]byte(content),
116					blackfriday.WithExtensions(blackfriday.CommonExtensions),
117				)
118				html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
119				readmeContent = template.HTML(html)
120			default:
121				readmeContent = template.HTML(
122					fmt.Sprintf(`<pre>%s</pre>`, content),
123				)
124			}
125			break
126		}
127	}
128
129	if readmeContent == "" {
130		log.Printf("no readme found for %s", name)
131	}
132
133	mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch)
134	if err != nil {
135		d.Write500(w)
136		log.Println(err)
137		return
138	}
139
140	tpath := filepath.Join(d.c.Dirs.Templates, "*")
141	t := template.Must(template.ParseGlob(tpath))
142
143	if len(commits) >= 3 {
144		commits = commits[:3]
145	}
146
147	data := make(map[string]any)
148	data["name"] = name
149	data["ref"] = mainBranch
150	data["readme"] = readmeContent
151	data["commits"] = commits
152	data["desc"] = getDescription(path)
153	data["servername"] = d.c.Server.Name
154	data["gomod"] = isGoModule(gr)
155
156	if err := t.ExecuteTemplate(w, "repo", data); err != nil {
157		log.Println(err)
158		return
159	}
160
161	return
162}
163
164func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
165	name := flow.Param(r.Context(), "name")
166	if d.isIgnored(name) {
167		d.Write404(w)
168		return
169	}
170	treePath := flow.Param(r.Context(), "...")
171	ref := flow.Param(r.Context(), "ref")
172
173	name = filepath.Clean(name)
174	path := filepath.Join(d.c.Repo.ScanPath, name)
175	gr, err := git.Open(path, ref)
176	if err != nil {
177		d.Write404(w)
178		return
179	}
180
181	files, err := gr.FileTree(treePath)
182	if err != nil {
183		d.Write500(w)
184		log.Println(err)
185		return
186	}
187
188	data := make(map[string]any)
189	data["name"] = name
190	data["ref"] = ref
191	data["parent"] = treePath
192	data["desc"] = getDescription(path)
193
194	d.listFiles(files, data, w)
195	return
196}
197
198func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
199	name := flow.Param(r.Context(), "name")
200	if d.isIgnored(name) {
201		d.Write404(w)
202		return
203	}
204	treePath := flow.Param(r.Context(), "...")
205	ref := flow.Param(r.Context(), "ref")
206
207	name = filepath.Clean(name)
208	path := filepath.Join(d.c.Repo.ScanPath, name)
209	gr, err := git.Open(path, ref)
210	if err != nil {
211		d.Write404(w)
212		return
213	}
214
215	contents, err := gr.FileContent(treePath)
216	data := make(map[string]any)
217	data["name"] = name
218	data["ref"] = ref
219	data["desc"] = getDescription(path)
220	data["path"] = treePath
221
222	d.showFile(contents, data, w)
223	return
224}
225
226func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
227	name := flow.Param(r.Context(), "name")
228	if d.isIgnored(name) {
229		d.Write404(w)
230		return
231	}
232	ref := flow.Param(r.Context(), "ref")
233
234	path := filepath.Join(d.c.Repo.ScanPath, name)
235	gr, err := git.Open(path, ref)
236	if err != nil {
237		d.Write404(w)
238		return
239	}
240
241	commits, err := gr.Commits()
242	if err != nil {
243		d.Write500(w)
244		log.Println(err)
245		return
246	}
247
248	tpath := filepath.Join(d.c.Dirs.Templates, "*")
249	t := template.Must(template.ParseGlob(tpath))
250
251	data := make(map[string]interface{})
252	data["commits"] = commits
253	data["meta"] = d.c.Meta
254	data["name"] = name
255	data["ref"] = ref
256	data["desc"] = getDescription(path)
257
258	if err := t.ExecuteTemplate(w, "log", data); err != nil {
259		log.Println(err)
260		return
261	}
262}
263
264func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
265	name := flow.Param(r.Context(), "name")
266	if d.isIgnored(name) {
267		d.Write404(w)
268		return
269	}
270	ref := flow.Param(r.Context(), "ref")
271
272	path := filepath.Join(d.c.Repo.ScanPath, name)
273	gr, err := git.Open(path, ref)
274	if err != nil {
275		d.Write404(w)
276		return
277	}
278
279	diff, err := gr.Diff()
280	if err != nil {
281		d.Write500(w)
282		log.Println(err)
283		return
284	}
285
286	tpath := filepath.Join(d.c.Dirs.Templates, "*")
287	t := template.Must(template.ParseGlob(tpath))
288
289	data := make(map[string]interface{})
290
291	data["commit"] = diff.Commit
292	data["stat"] = diff.Stat
293	data["diff"] = diff.Diff
294	data["meta"] = d.c.Meta
295	data["name"] = name
296	data["ref"] = ref
297	data["desc"] = getDescription(path)
298
299	if err := t.ExecuteTemplate(w, "commit", data); err != nil {
300		log.Println(err)
301		return
302	}
303}
304
305func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
306	name := flow.Param(r.Context(), "name")
307	if d.isIgnored(name) {
308		d.Write404(w)
309		return
310	}
311
312	path := filepath.Join(d.c.Repo.ScanPath, name)
313	gr, err := git.Open(path, "")
314	if err != nil {
315		d.Write404(w)
316		return
317	}
318
319	tags, err := gr.Tags()
320	if err != nil {
321		// Non-fatal, we *should* have at least one branch to show.
322		log.Println(err)
323	}
324
325	branches, err := gr.Branches()
326	if err != nil {
327		log.Println(err)
328		d.Write500(w)
329		return
330	}
331
332	tpath := filepath.Join(d.c.Dirs.Templates, "*")
333	t := template.Must(template.ParseGlob(tpath))
334
335	data := make(map[string]interface{})
336
337	data["meta"] = d.c.Meta
338	data["name"] = name
339	data["branches"] = branches
340	data["tags"] = tags
341	data["desc"] = getDescription(path)
342
343	if err := t.ExecuteTemplate(w, "refs", data); err != nil {
344		log.Println(err)
345		return
346	}
347}
348
349func (d *deps) ServeStatic(w http.ResponseWriter, r *http.Request) {
350	f := flow.Param(r.Context(), "file")
351	f = filepath.Clean(filepath.Join(d.c.Dirs.Static, f))
352
353	http.ServeFile(w, r, f)
354}