2021-12-08 10:09:47 +01:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2022-01-17 13:31:40 +01:00
|
|
|
"os"
|
2022-01-31 15:14:37 +01:00
|
|
|
|
|
|
|
"github.com/ClusterCockpit/cc-backend/log"
|
2021-12-08 10:09:47 +01:00
|
|
|
)
|
|
|
|
|
2022-01-10 16:14:54 +01:00
|
|
|
var templatesDir string
|
2022-01-17 13:31:40 +01:00
|
|
|
var debugMode bool = os.Getenv("DEBUG") == "1"
|
2022-01-10 16:14:54 +01:00
|
|
|
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
|
2022-02-03 09:39:04 +01:00
|
|
|
Error string
|
|
|
|
Info string
|
2021-12-09 16:27:48 +01:00
|
|
|
FilterPresets map[string]interface{}
|
|
|
|
Infos map[string]interface{}
|
|
|
|
Config map[string]interface{}
|
2021-12-08 10:09:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2022-02-10 18:48:58 +01:00
|
|
|
bp := "./"
|
|
|
|
ebp := os.Getenv("BASEPATH")
|
|
|
|
|
|
|
|
if ebp != "" {
|
|
|
|
bp = ebp
|
|
|
|
}
|
|
|
|
templatesDir = bp + "templates/"
|
2022-02-01 17:48:56 +01:00
|
|
|
base := template.Must(template.ParseFiles(templatesDir + "base.tmpl"))
|
2022-01-10 16:14:54 +01:00
|
|
|
files := []string{
|
2022-02-01 17:48:56 +01:00
|
|
|
"home.tmpl", "404.tmpl", "login.tmpl",
|
2022-02-03 09:39:04 +01:00
|
|
|
"imprint.tmpl", "privacy.tmpl",
|
2022-02-01 17:48:56 +01:00
|
|
|
"monitoring/jobs.tmpl",
|
|
|
|
"monitoring/job.tmpl",
|
2022-02-10 18:48:58 +01:00
|
|
|
"monitoring/taglist.tmpl",
|
2022-02-01 17:48:56 +01:00
|
|
|
"monitoring/list.tmpl",
|
|
|
|
"monitoring/user.tmpl",
|
2022-02-02 10:12:16 +01:00
|
|
|
"monitoring/systems.tmpl",
|
|
|
|
"monitoring/node.tmpl",
|
2022-02-03 10:42:15 +01:00
|
|
|
"monitoring/analysis.tmpl",
|
2022-01-10 16:14:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
templates[file] = template.Must(template.Must(base.Clone()).ParseFiles(templatesDir + file))
|
2021-12-08 15:50:03 +01:00
|
|
|
}
|
2021-12-08 10:09:47 +01:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:14:54 +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!")
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:14:54 +01:00
|
|
|
if debugMode {
|
2022-02-01 17:48:56 +01:00
|
|
|
t = template.Must(template.ParseFiles(templatesDir+"base.tmpl", templatesDir+file))
|
2022-01-10 16:14:54 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 16:27:48 +01:00
|
|
|
if err := t.Execute(rw, page); err != nil {
|
2022-01-31 15:14:37 +01:00
|
|
|
log.Errorf("template error: %s", err.Error())
|
2021-12-08 10:09:47 +01:00
|
|
|
}
|
|
|
|
}
|