Viewing:
package h import ( "compress/gzip" "embed" "fmt" "html/template" "io" "net/http" "strings" "git.awl.red/~neallred/recipes/htp" "git.awl.red/~neallred/recipes/logging" "git.awl.red/~neallred/recipes/model" "git.awl.red/~neallred/recipes/nav" ) //go:embed *.html var f embed.FS type FullPageProps struct { HeadTitle string NavItems []nav.Item } func (f *FullPageProps) Nav(userAuth model.Auther) *FullPageProps { f.NavItems = nav.GetItems(userAuth) return f } func (f *FullPageProps) Title(title string) *FullPageProps { f.HeadTitle = title return f } var tmpl = map[string]*template.Template{ "index.html": template.Must(template.ParseFS(f, "index.html", "base.html")), "recipes_search.html": template.Must(template.ParseFS(f, "recipes_search.html")), "signup.html": template.Must(template.ParseFS(f, "signup.html", "base.html")), "login.html": template.Must(template.ParseFS(f, "login.html", "base.html")), "recipe_details.html": template.Must(template.ParseFS(f, "recipe_details.html", "base.html")), "recipe_edit.html": template.Must(template.ParseFS(f, "recipe_edit.html", "base.html")), "recipes_for_later.html": template.Must(template.ParseFS(f, "recipes_for_later.html", "base.html")), "recipes_for_later_edit.html": template.Must(template.ParseFS(f, "recipes_for_later_edit.html", "base.html")), "new_recipe.html": template.Must(template.ParseFS(f, "new_recipe.html", "base.html")), "user_edit.html": template.Must(template.ParseFS(f, "user_edit.html", "base.html")), } func Write(w http.ResponseWriter, r *http.Request, templateName string, data any) { log := logging.FromReq(r) tmpl, ok := tmpl[templateName] if !ok { htp.W500(w, log, fmt.Errorf("Could not find template %q", templateName), "leiger", htp.BugTemplate) return } var err error desiredEncodings := r.Header.Get("Accept-Encoding") var ww io.Writer = w if strings.Contains(desiredEncodings, "gzip") { w.Header().Add("Content-Encoding", "gzip") gzw := gzip.NewWriter(w) defer gzw.Close() ww = gzw } if templateName == "recipes_search.html" { err = tmpl.Execute(ww, data) } else { err = tmpl.ExecuteTemplate(ww, "base", data) } if err != nil { log.Error("err sending template", "template", templateName, "err", err) } }