2024-02-11 19:28:00 +00:00
|
|
|
package templates
|
2024-02-10 11:59:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2024-02-20 10:24:04 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2024-02-22 19:20:34 +00:00
|
|
|
"strings"
|
2024-02-20 10:24:04 +00:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-02-10 11:59:58 +00:00
|
|
|
)
|
|
|
|
|
2024-02-11 10:56:21 +00:00
|
|
|
//go:embed *
|
2024-02-20 10:24:04 +00:00
|
|
|
var templates embed.FS
|
|
|
|
|
|
|
|
const base = "components/base.tmpl"
|
|
|
|
|
|
|
|
type Templates struct {
|
|
|
|
templates map[string]*template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
func load(files ...string) *template.Template {
|
|
|
|
files = append(files, base)
|
2024-02-22 19:20:34 +00:00
|
|
|
|
|
|
|
t := template.New("default").Funcs(
|
|
|
|
template.FuncMap{
|
|
|
|
"StringsJoin": strings.Join,
|
|
|
|
})
|
|
|
|
|
|
|
|
return template.Must(t.ParseFS(templates, files...))
|
2024-02-20 10:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadSettings(files ...string) *template.Template {
|
|
|
|
files = append(files, "components/settings.tmpl")
|
|
|
|
return load(files...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTemplates() *Templates {
|
|
|
|
return &Templates{
|
|
|
|
templates: map[string]*template.Template{
|
|
|
|
"404.tmpl": load("pages/404.tmpl"),
|
|
|
|
"index.tmpl": load("pages/index.tmpl"),
|
2024-02-23 08:36:13 +00:00
|
|
|
"incidents.tmpl": load("pages/incidents.tmpl"),
|
2024-02-20 10:24:04 +00:00
|
|
|
"settings_overview.tmpl": loadSettings("pages/settings_overview.tmpl"),
|
|
|
|
"settings_workers.tmpl": loadSettings("pages/settings_workers.tmpl"),
|
|
|
|
"settings_workers_create.tmpl": loadSettings("pages/settings_workers_create.tmpl"),
|
|
|
|
"settings_healthchecks.tmpl": loadSettings("pages/settings_healthchecks.tmpl"),
|
|
|
|
"settings_healthchecks_create.tmpl": loadSettings("pages/settings_healthchecks_create.tmpl"),
|
|
|
|
"settings_healthchecks_describe.tmpl": loadSettings("pages/settings_healthchecks_describe.tmpl"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Templates) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
|
|
if t.templates[name] == nil {
|
|
|
|
log.Printf("template not found: %s", name)
|
|
|
|
return echo.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
err := t.templates[name].ExecuteTemplate(w, "base", data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error rendering template: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|