all repos — legit @ c165c447685d68c2b0b2293a31937a903394f943

web frontend for git, written in go

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/config"
10	"icyphox.sh/legit/git"
11)
12
13func Write404(w http.ResponseWriter, c config.Config) {
14	w.WriteHeader(404)
15	tpath := filepath.Join(c.Template.Dir, "404.html")
16	t := template.Must(template.ParseFiles(tpath))
17	t.Execute(w, nil)
18}
19
20func Write500(w http.ResponseWriter, c config.Config) {
21	w.WriteHeader(500)
22	tpath := filepath.Join(c.Template.Dir, "500.html")
23	t := template.Must(template.ParseFiles(tpath))
24	t.Execute(w, nil)
25}
26
27func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) {
28	tpath := filepath.Join(d.c.Template.Dir, "*")
29	t := template.Must(template.ParseGlob(tpath))
30
31	data["files"] = files
32	data["meta"] = d.c.Meta
33
34	if err := t.ExecuteTemplate(w, "repo", data); err != nil {
35		log.Println(err)
36		return
37	}
38}
39
40func (d *deps) showFile(content string, data map[string]any, w http.ResponseWriter) {
41	tpath := filepath.Join(d.c.Template.Dir, "*")
42	t := template.Must(template.ParseGlob(tpath))
43
44	// TODO: Process content here.
45
46	data["content"] = content
47	data["meta"] = d.c.Meta
48
49	if err := t.ExecuteTemplate(w, "file", data); err != nil {
50		log.Println(err)
51		return
52	}
53}