2024-10-16 04:34:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-23 05:25:30 +00:00
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.32bit.cafe/32bitcafe/guestbook/internal/models"
|
2024-11-11 19:55:01 +00:00
|
|
|
"github.com/justinas/nosurf"
|
2024-10-16 04:34:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type templateData struct {
|
|
|
|
CurrentYear int
|
2024-10-23 05:25:30 +00:00
|
|
|
User models.User
|
|
|
|
Users []models.User
|
|
|
|
Guestbook models.Guestbook
|
|
|
|
Guestbooks []models.Guestbook
|
|
|
|
Comment models.GuestbookComment
|
|
|
|
Comments []models.GuestbookComment
|
|
|
|
Flash string
|
2024-11-11 19:55:01 +00:00
|
|
|
Form any
|
|
|
|
IsAuthenticated bool
|
|
|
|
CSRFToken string
|
2024-12-15 17:23:53 +00:00
|
|
|
CurrentUser *models.User
|
2024-10-16 04:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func humanDate(t time.Time) string {
|
|
|
|
return t.Format("02 Jan 2006 at 15:04")
|
|
|
|
}
|
|
|
|
|
|
|
|
var functions = template.FuncMap {
|
|
|
|
"humanDate": humanDate,
|
2024-11-11 19:55:01 +00:00
|
|
|
"shortIdToSlug": shortIdToSlug,
|
|
|
|
"slugToShortId": slugToShortId,
|
2024-10-16 04:34:57 +00:00
|
|
|
}
|
|
|
|
|
2024-12-15 17:23:53 +00:00
|
|
|
func newHTMXTemplateCache() (map[string]*template.Template, error) {
|
|
|
|
cache := map[string]*template.Template{}
|
|
|
|
pages, err := filepath.Glob("./ui/html/htmx/*.part.html")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, page := range pages {
|
|
|
|
name := filepath.Base(page)
|
|
|
|
ts, err := template.New(name).Funcs(functions).ParseFiles(page)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cache[name] = ts
|
|
|
|
}
|
|
|
|
return cache, nil
|
|
|
|
}
|
|
|
|
|
2024-10-16 04:34:57 +00:00
|
|
|
func newTemplateCache() (map[string]*template.Template, error) {
|
|
|
|
cache := map[string]*template.Template{}
|
|
|
|
pages, err := filepath.Glob("./ui/html/pages/*.tmpl.html")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, page := range pages {
|
|
|
|
name := filepath.Base(page)
|
|
|
|
ts, err := template.New(name).Funcs(functions).ParseFiles("./ui/html/base.tmpl.html")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ts, err = ts.ParseGlob("./ui/html/partials/*.tmpl.html")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ts, err = ts.ParseFiles(page)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cache[name] = ts
|
|
|
|
}
|
|
|
|
return cache, nil
|
|
|
|
}
|
|
|
|
|
2024-10-23 05:25:30 +00:00
|
|
|
func (app *application) newTemplateData(r *http.Request) templateData {
|
|
|
|
return templateData {
|
2024-10-16 04:34:57 +00:00
|
|
|
CurrentYear: time.Now().Year(),
|
2024-10-23 05:25:30 +00:00
|
|
|
Flash: app.sessionManager.PopString(r.Context(), "flash"),
|
2024-11-11 19:55:01 +00:00
|
|
|
IsAuthenticated: app.isAuthenticated(r),
|
|
|
|
CSRFToken: nosurf.Token(r),
|
2024-12-15 17:23:53 +00:00
|
|
|
CurrentUser: app.getCurrentUser(r),
|
2024-10-16 04:34:57 +00:00
|
|
|
}
|
|
|
|
}
|