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"
|
2024-02-29 14:12:20 +00:00
|
|
|
"time"
|
2024-02-20 10:24:04 +00:00
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-05-23 16:33:30 +00:00
|
|
|
"github.com/mentos1386/zdravko/pkg/script"
|
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{
|
2024-05-27 18:44:40 +00:00
|
|
|
"MathDivide": func(a, b int) float64 {
|
|
|
|
if b == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return float64(a) / float64(b)
|
|
|
|
},
|
2024-05-25 17:52:37 +00:00
|
|
|
"DurationRoundSecond": func(d time.Duration) time.Duration {
|
|
|
|
return d.Round(time.Second)
|
|
|
|
},
|
|
|
|
"DurationRoundMillisecond": func(d time.Duration) time.Duration {
|
|
|
|
return d.Round(time.Millisecond)
|
|
|
|
},
|
2024-05-14 19:38:45 +00:00
|
|
|
"StringsJoin": strings.Join,
|
|
|
|
"Now": time.Now,
|
|
|
|
"ScriptUnescapeString": script.UnescapeString,
|
|
|
|
"ScriptEscapeString": script.EscapeString,
|
2024-02-22 19:20:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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{
|
2024-02-24 21:07:49 +00:00
|
|
|
"404.tmpl": load("pages/404.tmpl"),
|
|
|
|
"index.tmpl": load("pages/index.tmpl"),
|
|
|
|
"incidents.tmpl": load("pages/incidents.tmpl"),
|
2024-05-17 19:54:14 +00:00
|
|
|
"settings_home.tmpl": loadSettings("pages/settings_home.tmpl"),
|
2024-04-28 19:24:00 +00:00
|
|
|
"settings_triggers.tmpl": loadSettings("pages/settings_triggers.tmpl"),
|
|
|
|
"settings_triggers_create.tmpl": loadSettings("pages/settings_triggers_create.tmpl"),
|
|
|
|
"settings_triggers_describe.tmpl": loadSettings("pages/settings_triggers_describe.tmpl"),
|
2024-05-14 19:38:45 +00:00
|
|
|
"settings_targets.tmpl": loadSettings("pages/settings_targets.tmpl"),
|
2024-05-23 16:33:30 +00:00
|
|
|
"settings_targets_create.tmpl": loadSettings("pages/settings_targets_create.tmpl"),
|
|
|
|
"settings_targets_describe.tmpl": loadSettings("pages/settings_targets_describe.tmpl"),
|
2024-05-16 20:15:14 +00:00
|
|
|
"settings_incidents.tmpl": loadSettings("pages/settings_incidents.tmpl"),
|
2024-03-01 09:53:36 +00:00
|
|
|
"settings_notifications.tmpl": loadSettings("pages/settings_notifications.tmpl"),
|
2024-02-24 21:07:49 +00:00
|
|
|
"settings_worker_groups.tmpl": loadSettings("pages/settings_worker_groups.tmpl"),
|
|
|
|
"settings_worker_groups_create.tmpl": loadSettings("pages/settings_worker_groups_create.tmpl"),
|
|
|
|
"settings_worker_groups_describe.tmpl": loadSettings("pages/settings_worker_groups_describe.tmpl"),
|
2024-05-16 20:15:14 +00:00
|
|
|
"settings_checks.tmpl": loadSettings("pages/settings_checks.tmpl"),
|
|
|
|
"settings_checks_create.tmpl": loadSettings("pages/settings_checks_create.tmpl"),
|
|
|
|
"settings_checks_describe.tmpl": loadSettings("pages/settings_checks_describe.tmpl"),
|
2024-02-20 10:24:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|