2024-04-29 08:36:13 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
// External
|
|
|
|
we "git.gibonuddevalla.se/go/wrappederror"
|
|
|
|
|
|
|
|
// Standard
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Page struct {
|
|
|
|
LAYOUT string
|
|
|
|
PAGE string
|
|
|
|
MENU string
|
2024-06-25 08:59:07 +02:00
|
|
|
CONFIG map[string]string
|
2024-04-29 08:36:13 +02:00
|
|
|
Label string
|
|
|
|
Icon string
|
|
|
|
|
|
|
|
Data any
|
|
|
|
}
|
|
|
|
|
2024-06-27 09:51:52 +02:00
|
|
|
func (p *Page) Render(w http.ResponseWriter, r *http.Request) {
|
2024-04-29 08:36:13 +02:00
|
|
|
tmpl, err := getPage(p.LAYOUT, p.PAGE)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, we.Wrap(err).Log())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Icon == "" {
|
|
|
|
p.Icon = p.PAGE
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Label == "" {
|
|
|
|
r, _ := utf8.DecodeRuneInString(p.PAGE)
|
|
|
|
titled := unicode.ToTitle(r)
|
|
|
|
p.Label = fmt.Sprintf("%s%s", string(titled), p.PAGE[1:len(p.PAGE)])
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.MENU == "" {
|
|
|
|
p.MENU = p.PAGE
|
|
|
|
}
|
|
|
|
|
|
|
|
data := map[string]any{
|
|
|
|
"VERSION": VERSION,
|
|
|
|
"LAYOUT": p.LAYOUT,
|
|
|
|
"PAGE": p.PAGE,
|
|
|
|
"MENU": p.MENU,
|
2024-06-25 08:59:07 +02:00
|
|
|
"CONFIG": smonConfig.Settings,
|
2024-06-27 09:51:52 +02:00
|
|
|
"ERROR": r.URL.Query().Get("_err"),
|
2024-06-25 08:59:07 +02:00
|
|
|
|
2024-04-29 08:36:13 +02:00
|
|
|
"Label": p.Label,
|
|
|
|
"Icon": p.Icon,
|
|
|
|
"Data": p.Data,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tmpl.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, we.Wrap(err).Log())
|
|
|
|
}
|
|
|
|
}
|