summaryrefslogtreecommitdiffstats
path: root/roseh.moe.go
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2026-09-08 19:23:55 -0700
committerRose Hogenson <rosehogenson@posteo.net>2026-09-08 19:24:32 -0700
commit137522267024aedd28e4a68540f2e0fba7a8aa2a (patch)
tree6801443511c049338c7cb3c260a9389627596f15 /roseh.moe.go
parentUse a relative link for projects (diff)
downloadroseh.moe-137522267024aedd28e4a68540f2e0fba7a8aa2a.tar.zst
Create static templates after parsing flags
Diffstat (limited to 'roseh.moe.go')
-rw-r--r--roseh.moe.go68
1 files changed, 44 insertions, 24 deletions
diff --git a/roseh.moe.go b/roseh.moe.go
index d03d106..5be22ce 100644
--- a/roseh.moe.go
+++ b/roseh.moe.go
@@ -214,20 +214,20 @@ func (h *staticHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.ServeContent(w, r, "", time.Time{}, strings.NewReader(h.content))
}
-func mustStaticTemplate(tmpl *template.Template) *staticHandler {
+func staticTemplate(tmpl *template.Template) (*staticHandler, error) {
buf := new(strings.Builder)
if err := tmpl.Execute(buf, nil); err != nil {
- panic(err)
+ return nil, err
}
content := buf.String()
eTag, err := makeETag(strings.NewReader(content))
if err != nil {
- panic(err)
+ return nil, err
}
return &staticHandler{
content: buf.String(),
eTag: eTag,
- }
+ }, nil
}
var (
@@ -246,29 +246,17 @@ func notFound(w http.ResponseWriter, r *http.Request) {
}
}
-var (
- //go:embed templates/index.html.template
- indexString string
- indexHandler = mustStaticTemplate(template.Must(outlinedTemplate(indexString)))
-)
+//go:embed templates/index.html.template
+var indexString string
-var (
- //go:embed templates/games/index.html.template
- gamesIndexString string
- gamesIndexHandler = mustStaticTemplate(template.Must(outlinedTemplate(gamesIndexString)))
-)
+//go:embed templates/games/index.html.template
+var gamesIndexString string
-var (
- //go:embed templates/games/pong.html.template
- pongString string
- pongHandler = mustStaticTemplate(template.Must(template.New("pong").Parse(pongString)))
-)
+//go:embed templates/games/pong.html.template
+var pongString string
-var (
- //go:embed templates/games/rms.html.template
- stallmanShooterString string
- stallmanShooterHandler = mustStaticTemplate(template.Must(outlinedTemplate(stallmanShooterString)))
-)
+//go:embed templates/games/rms.html.template
+var stallmanShooterString string
//go:embed static
var staticFiles embed.FS
@@ -1093,9 +1081,41 @@ func main() {
}()
mux := http.NewServeMux()
+ indexTemplate, err := outlinedTemplate(indexString)
+ if err != nil {
+ log.Fatal(err)
+ }
+ indexHandler, err := staticTemplate(indexTemplate)
+ if err != nil {
+ log.Fatal(err)
+ }
mux.Handle("GET /{$}", indexHandler)
+ gamesIndexTemplate, err := outlinedTemplate(gamesIndexString)
+ if err != nil {
+ log.Fatal(err)
+ }
+ gamesIndexHandler, err := staticTemplate(gamesIndexTemplate)
+ if err != nil {
+ log.Fatal(err)
+ }
mux.Handle("GET /games/{$}", gamesIndexHandler)
+ pongTemplate, err := template.New("pong").Parse(pongString)
+ if err != nil {
+ log.Fatal(err)
+ }
+ pongHandler, err := staticTemplate(pongTemplate)
+ if err != nil {
+ log.Fatal(err)
+ }
mux.Handle("GET /games/pong", pongHandler)
+ stallmanShooterTemplate, err := outlinedTemplate(stallmanShooterString)
+ if err != nil {
+ log.Fatal(err)
+ }
+ stallmanShooterHandler, err := staticTemplate(stallmanShooterTemplate)
+ if err != nil {
+ log.Fatal(err)
+ }
mux.Handle("GET /games/rms", stallmanShooterHandler)
mux.HandleFunc("GET /qr.png", qrHandler)
mux.HandleFunc("GET /wormhole", wormhole)