diff options
Diffstat (limited to 'roseh.moe.go')
| -rw-r--r-- | roseh.moe.go | 68 |
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) |
