cc-backend/templates/templates.go

57 lines
1.3 KiB
Go
Raw Normal View History

2021-12-08 10:09:47 +01:00
package templates
import (
"html/template"
"log"
"net/http"
)
var templatesDir string
var debugMode bool = true
var templates map[string]*template.Template = 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() {
templatesDir = "./templates/"
base := template.Must(template.ParseFiles(templatesDir + "base.html"))
files := []string{
"home.html", "404.html", "login.html",
"monitoring/jobs.html", "monitoring/job.html",
"monitoring/users.html", "monitoring/user.html",
"monitoring/analysis.html",
"monitoring/systems.html",
"monitoring/node.html",
}
for _, file := range files {
templates[file] = template.Must(template.Must(base.Clone()).ParseFiles(templatesDir + file))
}
2021-12-08 10:09:47 +01:00
}
func Render(rw http.ResponseWriter, r *http.Request, file string, page *Page) {
t, ok := templates[file]
2021-12-09 16:27:48 +01:00
if !ok {
panic("templates must be predefinied!")
}
if debugMode {
t = template.Must(template.ParseFiles(templatesDir+"base.html", templatesDir+file))
}
2021-12-09 16:27:48 +01:00
if err := t.Execute(rw, page); err != nil {
2021-12-08 10:09:47 +01:00
log.Printf("template error: %s\n", err.Error())
}
}