legit/routes/template.go

57 lines
1.3 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"
2022-12-11 08:48:39 +00:00
"icyphox.sh/legit/git"
2022-12-11 05:52:47 +00:00
)
2022-12-12 15:23:58 +00:00
func (d *deps) Write404(w http.ResponseWriter) {
tpath := filepath.Join(d.c.Template.Dir, "*")
t := template.Must(template.ParseGlob(tpath))
2022-12-11 05:52:47 +00:00
w.WriteHeader(404)
2022-12-12 15:23:58 +00:00
if err := t.ExecuteTemplate(w, "404", nil); err != nil {
log.Printf("404 template: %s", err)
}
2022-12-11 05:52:47 +00:00
}
2022-12-12 15:23:58 +00:00
func (d *deps) Write500(w http.ResponseWriter) {
tpath := filepath.Join(d.c.Template.Dir, "*")
t := template.Must(template.ParseGlob(tpath))
2022-12-11 05:52:47 +00:00
w.WriteHeader(500)
2022-12-12 15:23:58 +00:00
if err := t.ExecuteTemplate(w, "500", nil); err != nil {
log.Printf("500 template: %s", err)
}
2022-12-11 05:52:47 +00:00
}
2022-12-11 15:47:04 +00:00
func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) {
2022-12-11 08:48:39 +00:00
tpath := filepath.Join(d.c.Template.Dir, "*")
t := template.Must(template.ParseGlob(tpath))
data["files"] = files
data["meta"] = d.c.Meta
if err := t.ExecuteTemplate(w, "repo", data); err != nil {
log.Println(err)
return
}
}
2022-12-11 15:47:04 +00:00
func (d *deps) showFile(content string, data map[string]any, w http.ResponseWriter) {
2022-12-11 08:48:39 +00:00
tpath := filepath.Join(d.c.Template.Dir, "*")
t := template.Must(template.ParseGlob(tpath))
2022-12-11 15:47:04 +00:00
// TODO: Process content here.
2022-12-11 08:48:39 +00:00
data["content"] = content
data["meta"] = d.c.Meta
if err := t.ExecuteTemplate(w, "file", data); err != nil {
log.Println(err)
return
2022-12-11 05:52:47 +00:00
}
}