cc-backend/templates/templates.go

50 lines
1.8 KiB
Go
Raw Normal View History

2021-12-08 10:09:47 +01:00
package templates
import (
"html/template"
"log"
"net/http"
)
var templates map[string]*template.Template
2021-12-08 10:09:47 +01:00
type Page struct {
2021-12-09 16:27:48 +01:00
Title string
Login *LoginPage
FilterPresets map[string]interface{}
Infos map[string]interface{}
Config map[string]interface{}
2021-12-08 10:09:47 +01:00
}
type LoginPage struct {
Error string
Info string
}
func init() {
base := template.Must(template.ParseFiles("./templates/base.html"))
templates = map[string]*template.Template{
2021-12-09 16:27:48 +01:00
"home": template.Must(template.Must(base.Clone()).ParseFiles("./templates/home.html")),
"404": template.Must(template.Must(base.Clone()).ParseFiles("./templates/404.html")),
"login": template.Must(template.Must(base.Clone()).ParseFiles("./templates/login.html")),
"monitoring/jobs/": template.Must(template.Must(base.Clone()).ParseFiles("./templates/monitoring/jobs.html")),
"monitoring/job/": template.Must(template.Must(base.Clone()).ParseFiles("./templates/monitoring/job.html")),
"monitoring/users/": template.Must(template.Must(base.Clone()).ParseFiles("./templates/monitoring/users.html")),
"monitoring/user/": template.Must(template.Must(base.Clone()).ParseFiles("./templates/monitoring/user.html")),
"monitoring/analysis/": template.Must(template.Must(base.Clone()).ParseFiles("./templates/monitoring/analysis.html")),
"monitoring/systems/": template.Must(template.Must(base.Clone()).ParseFiles("./templates/monitoring/systems.html")),
"monitoring/node/": template.Must(template.Must(base.Clone()).ParseFiles("./templates/monitoring/node.html")),
}
2021-12-08 10:09:47 +01:00
}
func Render(rw http.ResponseWriter, r *http.Request, name string, page *Page) {
2021-12-09 16:27:48 +01:00
t, ok := templates[name]
if !ok {
panic("templates must be predefinied!")
}
if err := t.Execute(rw, page); err != nil {
2021-12-08 10:09:47 +01:00
log.Printf("template error: %s\n", err.Error())
}
}