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, w http.ResponseWriter) {
28 tpath := filepath.Join(d.c.Template.Dir, "*")
29 t := template.Must(template.ParseGlob(tpath))
30
31 data := make(map[string]interface{})
32 data["files"] = files
33 data["meta"] = d.c.Meta
34
35 if err := t.ExecuteTemplate(w, "repo", data); err != nil {
36 Write500(w, *d.c)
37 log.Println(err)
38 return
39 }
40}
41
42func (d *deps) showFile(content string, w http.ResponseWriter) {
43 tpath := filepath.Join(d.c.Template.Dir, "*")
44 t := template.Must(template.ParseGlob(tpath))
45
46 data := make(map[string]interface{})
47 data["content"] = content
48 data["meta"] = d.c.Meta
49
50 if err := t.ExecuteTemplate(w, "file", data); err != nil {
51 Write500(w, *d.c)
52 log.Println(err)
53 return
54 }
55}