Spaces:
Running
Running
File size: 3,150 Bytes
84ffb7a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | package web
import (
"embed"
"fmt"
"html/template"
"io/fs"
"log/slog"
"net/http"
"strings"
"time"
)
//go:embed templates static
var content embed.FS
// funcs are shared by every template set.
var funcs = template.FuncMap{
"stars": stars,
"timefmt": timefmt,
}
// pageNames are the standalone pages, each parsed into its own template set so
// their "content" block definitions don't collide with each other.
var pageNames = []string{"home", "login", "markitdown"}
// fragments holds base.html plus all partials, shared across pages.
var fragments *template.Template
// pages maps a page name to a template set containing base + partials + page.
var pages map[string]*template.Template
func init() {
var err error
fragments, err = template.New("").Funcs(funcs).ParseFS(content,
"templates/*.html",
)
if err != nil {
panic(fmt.Sprintf("parse base/partials templates: %v", err))
}
pages = make(map[string]*template.Template, len(pageNames))
for _, name := range pageNames {
// Clone the shared set, then parse this page's file into the clone.
set, err := fragments.Clone()
if err != nil {
panic(fmt.Sprintf("clone template set: %v", err))
}
body, err := content.ReadFile("templates/pages/" + name + ".html")
if err != nil {
panic(fmt.Sprintf("read page %s: %v", name, err))
}
if _, err := set.New(name).Parse(string(body)); err != nil {
panic(fmt.Sprintf("parse page %s: %v", name, err))
}
pages[name] = set
}
}
// templateLog is set by NewServer so render failures are visible in logs.
var templateLog = slog.Default()
// render executes a named template with data. Page names resolve to their own
// template set; anything else is treated as a fragment from the shared set.
func render(w http.ResponseWriter, r *http.Request, name string, data any) {
if set, ok := pages[name]; ok {
if err := set.ExecuteTemplate(w, name, data); err != nil {
templateLog.Error("template execution failed", "template", name, "error", err)
http.Error(w, "template error", http.StatusInternalServerError)
}
return
}
if err := fragments.ExecuteTemplate(w, name, data); err != nil {
templateLog.Error("template execution failed", "template", name, "error", err)
http.Error(w, "template error", http.StatusInternalServerError)
}
}
// staticFiles serves the embedded static assets with long-lived caching.
func staticFiles() http.Handler {
sub, err := fs.Sub(content, "static")
if err != nil {
panic(fmt.Sprintf("embed static: %v", err))
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, ".js") || strings.HasSuffix(r.URL.Path, ".css") {
// Versioned URLs (?v=) carry the cache-busting; revalidate often.
w.Header().Set("Cache-Control", "public, max-age=3600, must-revalidate")
}
http.FileServer(http.FS(sub)).ServeHTTP(w, r)
})
}
// stars renders a 1-5 star rating as plain text (safe, no escaping needed).
func stars(n int) string {
if n < 0 {
n = 0
}
if n > 5 {
n = 5
}
return strings.Repeat("★", n) + strings.Repeat("☆", 5-n)
}
func timefmt(t time.Time) string {
return t.Format("Jan 2, 2006")
}
|