all repos — legit @ 60e1092dbcfacb28b30d052b0f73d2249cd63a9d

web frontend for git

routes/template.go (view raw)

 1package routes
 2
 3import (
 4	"html/template"
 5	"log"
 6	"net/http"
 7	"path/filepath"
 8
 9	"icyphox.sh/legit/git"
10)
11
12func (d *deps) Write404(w http.ResponseWriter) {
13	tpath := filepath.Join(d.c.Template.Dir, "*")
14	t := template.Must(template.ParseGlob(tpath))
15	w.WriteHeader(404)
16	if err := t.ExecuteTemplate(w, "404", nil); err != nil {
17		log.Printf("404 template: %s", err)
18	}
19}
20
21func (d *deps) Write500(w http.ResponseWriter) {
22	tpath := filepath.Join(d.c.Template.Dir, "*")
23	t := template.Must(template.ParseGlob(tpath))
24	w.WriteHeader(500)
25	if err := t.ExecuteTemplate(w, "500", nil); err != nil {
26		log.Printf("500 template: %s", err)
27	}
28}
29
30func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) {
31	tpath := filepath.Join(d.c.Template.Dir, "*")
32	t := template.Must(template.ParseGlob(tpath))
33
34	data["files"] = files
35	data["meta"] = d.c.Meta
36
37	if err := t.ExecuteTemplate(w, "repo", data); err != nil {
38		log.Println(err)
39		return
40	}
41}
42
43func (d *deps) showFile(content string, data map[string]any, w http.ResponseWriter) {
44	tpath := filepath.Join(d.c.Template.Dir, "*")
45	t := template.Must(template.ParseGlob(tpath))
46
47	// TODO: Process content here.
48
49	data["content"] = content
50	data["meta"] = d.c.Meta
51
52	if err := t.ExecuteTemplate(w, "file", data); err != nil {
53		log.Println(err)
54		return
55	}
56}