summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-10-23 17:24:54 -0700
committerRose Hogenson <rosehogenson@posteo.net>2024-10-23 17:24:54 -0700
commit9c7895f5cdd27aeadcaa7128682a047cfc4fb3f8 (patch)
tree095be38789871a82d1aebd0ed3cf2547b7b850db
parent44645b7ec78750ee61e9f65313242788c324437d (diff)
downloadroseh.moe-9c7895f5cdd27aeadcaa7128682a047cfc4fb3f8.tar.zst
Don't pre-render templates.
-rw-r--r--roseh.moe.go146
-rw-r--r--templates/blog/post.html.template1
-rw-r--r--templates/blog/posts/2024-10-22_nix_flakes.html.template2
-rw-r--r--templates/index.html.template6
-rw-r--r--templates/outline.html.template2
5 files changed, 64 insertions, 93 deletions
diff --git a/roseh.moe.go b/roseh.moe.go
index 932579f..98febd1 100644
--- a/roseh.moe.go
+++ b/roseh.moe.go
@@ -1,7 +1,6 @@
package main
import (
- "bytes"
"embed"
"flag"
"fmt"
@@ -55,128 +54,97 @@ var (
blogPostFiles embed.FS
)
-func posts() []string {
- postFiles, err := blogPostFiles.ReadDir("templates/blog/posts")
- if err != nil {
- log.Fatalf("blog posts: %s", err)
- }
- posts := make([]string, len(postFiles))
- for i, f := range postFiles {
- posts[i] = strings.TrimSuffix(f.Name(), ".html.template")
- }
- return posts
-}
-
-func postName(post string) string {
- postBytes, err := blogPostFiles.ReadFile("templates/blog/posts/" + post + ".html.template")
- if err != nil {
- log.Fatalf("blog post %q: %s", post, err)
- }
- return string(bytes.TrimSuffix(bytes.TrimPrefix(postBytes[:bytes.IndexByte(postBytes, '\n')], []byte("<h1>")), []byte("</h1>")))
-}
-
-func preloadBlogPosts() map[string][]byte {
+func mustParseBlogPostTemplates() map[string]*template.Template {
base, err := outlineTemplate.Clone()
if err != nil {
log.Fatalf("outlineTemplate.Clone: %s", err)
}
- base, err = base.New("body").Parse(postString)
+ if _, err := base.New("body").Parse(postString); err != nil {
+ log.Fatalf("post.html.template: %s", err)
+ }
+ postFiles, err := blogPostFiles.ReadDir("templates/blog/posts")
if err != nil {
- log.Fatalf("post.html.template: invalid template: %s", err)
+ log.Fatalf("read blog post templates: %s", err)
}
- postSlugs := posts()
- posts := make(map[string][]byte, len(postSlugs))
- for _, post := range postSlugs {
- postTemplate, err := base.Clone()
- if err != nil {
- log.Fatalf("base.Clone: %s", err)
- }
- postString, err := blogPostFiles.ReadFile("templates/blog/posts/" + post + ".html.template")
+ posts := make(map[string]*template.Template, len(postFiles))
+ for _, file := range postFiles {
+ fileContent, err := blogPostFiles.ReadFile("templates/blog/posts/" + file.Name())
if err != nil {
- log.Fatalf("somehow I fucked the paths up: %s", err)
+ log.Fatalf("read blog post template %q: %s", file.Name(), err)
}
- postTemplate, err = postTemplate.New("post").Parse(string(postString))
+ tmpl, err := base.Clone()
if err != nil {
- log.Fatalf("%q: invalid template: %s", post, err)
- }
- type args struct {
- Title string
+ log.Fatalf("base.Clone: %s", err)
}
- buf := new(bytes.Buffer)
- if err := postTemplate.ExecuteTemplate(buf, "outline", args{Title: postName(post)}); err != nil {
- log.Fatalf("%q: error: %s", post, err)
+ if _, err := tmpl.New("post").Parse(string(fileContent)); err != nil {
+ log.Fatalf("%q: %s", file.Name(), err)
}
- posts[post] = buf.Bytes()
+ posts[strings.TrimSuffix(file.Name(), ".html.template")] = tmpl
}
return posts
}
-var blogPostsHTML = preloadBlogPosts()
+var blogPostTemplates = mustParseBlogPostTemplates()
func blog(w http.ResponseWriter, r *http.Request) {
- post, ok := blogPostsHTML[strings.TrimPrefix(r.URL.Path, "/blog/")]
+ tmpl, ok := blogPostTemplates[strings.TrimPrefix(r.URL.Path, "/blog/")]
if !ok {
notFound(w, r)
return
}
- http.ServeContent(w, r, "post.html", serverStartTime, bytes.NewReader(post))
+ if err := tmpl.ExecuteTemplate(w, "outline", nil); err != nil {
+ log.Printf("Warning: blog: %s", err)
+ }
}
-//go:embed templates/index.html.template
-var indexString string
+type blogPost struct {
+ Name string
+ Slug string
+}
-func preloadIndex() []byte {
- base, err := outlineTemplate.Clone()
- if err != nil {
- log.Fatalf("outlineTemplate.Clone: %s", err)
- }
- base.Funcs(template.FuncMap{
- "name": func(post string) string {
- return post[:strings.IndexByte(post, '_')] + ": " + postName(post)
- },
- })
- indexTemplate, err := base.New("body").Parse(indexString)
- if err != nil {
- log.Fatalf("index.html.template: invalid template: %s", err)
- }
- posts := posts()
- slices.SortFunc(posts, func(x, y string) int { return strings.Compare(y, x) })
- type args struct {
- Title string
- Posts []string
- }
- buf := new(bytes.Buffer)
- if err := indexTemplate.ExecuteTemplate(buf, "outline", args{Title: "roseh.moe", Posts: posts}); err != nil {
- log.Fatalf("index.html.template: error: %s", err)
+func mustLoadBlogPosts() []blogPost {
+ buf := new(strings.Builder)
+ posts := make([]blogPost, 0, len(blogPostTemplates))
+ for slug, tmpl := range blogPostTemplates {
+ buf.Reset()
+ if err := tmpl.ExecuteTemplate(buf, "title", nil); err != nil {
+ log.Fatalf("blog post %q: title: %s", slug, err)
+ }
+ posts = append(posts, blogPost{
+ Name: buf.String(),
+ Slug: slug,
+ })
}
- return buf.Bytes()
+ slices.SortFunc(posts, func(x, y blogPost) int { return strings.Compare(y.Slug, x.Slug) })
+ return posts
}
-var indexHTML = preloadIndex()
+var (
+ //go:embed templates/index.html.template
+ indexString string
-func index(w http.ResponseWriter, r *http.Request) {
- http.ServeContent(w, r, "index.html", serverStartTime, bytes.NewReader(indexHTML))
-}
+ indexTemplate = template.Must(template.Must(outlineTemplate.Clone()).New("body").Parse(indexString))
-//go:embed templates/pong.html.template
-var pongString string
+ blogPostsList = mustLoadBlogPosts()
+)
-func preloadPong() []byte {
- pongTemplate, err := template.New("pong").Parse(pongString)
- if err != nil {
- log.Fatalf("pong.html.template: invalid template: %s", err)
+func index(w http.ResponseWriter, _ *http.Request) {
+ if err := indexTemplate.ExecuteTemplate(w, "outline", blogPostsList); err != nil {
+ log.Printf("Warning: index: %s", err)
}
- buf := new(bytes.Buffer)
- if err := pongTemplate.Execute(buf, nil); err != nil {
- log.Fatalf("pong.html.template: error: %s", err)
- }
- return buf.Bytes()
}
-var pongHTML = preloadPong()
+var (
+ //go:embed templates/pong.html.template
+ pongString string
-func pong(w http.ResponseWriter, r *http.Request) {
- http.ServeContent(w, r, "pong.html", serverStartTime, bytes.NewReader(pongHTML))
+ pongTemplate = template.Must(template.New("pong").Parse(pongString))
+)
+
+func pong(w http.ResponseWriter, _ *http.Request) {
+ if err := pongTemplate.Execute(w, nil); err != nil {
+ log.Printf("Warning: pong: %s", err)
+ }
}
//go:embed static
diff --git a/templates/blog/post.html.template b/templates/blog/post.html.template
index ba5e81f..d250fee 100644
--- a/templates/blog/post.html.template
+++ b/templates/blog/post.html.template
@@ -1,4 +1,5 @@
<article>
<a href="/">⬅️Home</a>
+ <h1>{{template "title" .}}</h1>
{{template "post"}}
</article>
diff --git a/templates/blog/posts/2024-10-22_nix_flakes.html.template b/templates/blog/posts/2024-10-22_nix_flakes.html.template
index b9b2ca0..d191908 100644
--- a/templates/blog/posts/2024-10-22_nix_flakes.html.template
+++ b/templates/blog/posts/2024-10-22_nix_flakes.html.template
@@ -1,4 +1,4 @@
-<h1>nix flakes for busy people</h1>
+{{define "title"}}nix flakes for busy people{{end}}
<h2>TL;DR</h2>
diff --git a/templates/index.html.template b/templates/index.html.template
index 5c8d11b..43e35f9 100644
--- a/templates/index.html.template
+++ b/templates/index.html.template
@@ -1,3 +1,5 @@
+{{define "title"}}roseh.moe{{end}}
+
<div style="background-image:url(/static/index/stars.gif);background-repeat:repeat;height:500px;display:flex;justify-content:center;align-items:center">
<h1>roseh.moe</h1>
</div>
@@ -10,8 +12,8 @@
<p><a href="/pong">Games?</a> (Warning: loud)</p>
</article>
-{{range .Posts}}
+{{range .}}
<article>
- <a href="/blog/{{.}}"><h2>{{name .}}</h2></a>
+ <a href="/blog/{{.Slug}}"><h2>{{.Name}}</h2></a>
</article>
{{end}}
diff --git a/templates/outline.html.template b/templates/outline.html.template
index be739c3..9dd6730 100644
--- a/templates/outline.html.template
+++ b/templates/outline.html.template
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
- <title>{{.Title}}</title>
+ <title>{{template "title" .}}</title>
<link rel="icon" href="/static/icon.png">
<link rel="stylesheet" href="/static/styles.css">
</head>