summaryrefslogtreecommitdiffstats
path: root/routes/template.go
blob: 5595f6ef56b599d468f8622ab4e05c371c973cef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package routes

import (
	"html/template"
	"log"
	"net/http"
	"path/filepath"

	"icyphox.sh/legit/config"
	"icyphox.sh/legit/git"
)

func Write404(w http.ResponseWriter, c config.Config) {
	w.WriteHeader(404)
	tpath := filepath.Join(c.Template.Dir, "404.html")
	t := template.Must(template.ParseFiles(tpath))
	t.Execute(w, nil)
}

func Write500(w http.ResponseWriter, c config.Config) {
	w.WriteHeader(500)
	tpath := filepath.Join(c.Template.Dir, "500.html")
	t := template.Must(template.ParseFiles(tpath))
	t.Execute(w, nil)
}

func (d *deps) listFiles(files []git.NiceTree, w http.ResponseWriter) {
	tpath := filepath.Join(d.c.Template.Dir, "*")
	t := template.Must(template.ParseGlob(tpath))

	data := make(map[string]interface{})
	data["files"] = files
	data["meta"] = d.c.Meta

	if err := t.ExecuteTemplate(w, "repo", data); err != nil {
		Write500(w, *d.c)
		log.Println(err)
		return
	}
}

func (d *deps) showFile(content string, w http.ResponseWriter) {
	tpath := filepath.Join(d.c.Template.Dir, "*")
	t := template.Must(template.ParseGlob(tpath))

	data := make(map[string]interface{})
	data["content"] = content
	data["meta"] = d.c.Meta

	if err := t.ExecuteTemplate(w, "file", data); err != nil {
		Write500(w, *d.c)
		log.Println(err)
		return
	}
}