legit/routes/template.go

56 lines
1.2 KiB
Go
Raw Normal View History

2022-12-11 05:52:47 +00:00
package routes
import (
"html/template"
2022-12-11 08:48:39 +00:00
"log"
2022-12-11 05:52:47 +00:00
"net/http"
"path/filepath"
"icyphox.sh/legit/config"
2022-12-11 08:48:39 +00:00
"icyphox.sh/legit/git"
2022-12-11 05:52:47 +00:00
)
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)
}
2022-12-11 08:48:39 +00:00
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
2022-12-11 05:52:47 +00:00
}
}