From 8cfead0704354399baecaf3e279c730bed78b359 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Thu, 23 Jul 2026 15:26:31 -0700 Subject: Add a helper function for making an outlined template --- roseh.moe.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'roseh.moe.go') diff --git a/roseh.moe.go b/roseh.moe.go index 20c2101..cef9657 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -145,6 +145,24 @@ func loadConfig() (*serviceConfiguration, error) { return config, nil } +var ( + //go:embed templates/outline.html.template + outlineString string + outlineTemplate = template.Must(template.New("outline").Parse(outlineString)) +) + +func outlinedTemplate(s string) (*template.Template, error) { + outline, err := outlineTemplate.Clone() + if err != nil { + return nil, err + } + t, err := outline.New("body").Parse(s) + if err != nil { + return nil, err + } + return t.Lookup("outline"), nil +} + var ( //go:embed templates/404.html.template notFoundString string @@ -162,13 +180,9 @@ func notFound(w http.ResponseWriter, r *http.Request) { } var ( - //go:embed templates/outline.html.template - outlineString string - outlineTemplate = template.Must(template.New("outline").Parse(outlineString)) - //go:embed templates/index.html.template indexString string - indexTemplate = template.Must(template.Must(outlineTemplate.Clone()).New("body").Parse(indexString)).Lookup("outline") + indexTemplate = template.Must(outlinedTemplate(indexString)) ) func index(w http.ResponseWriter, _ *http.Request) { @@ -233,7 +247,7 @@ func qrHandler(w http.ResponseWriter, r *http.Request) { var ( //go:embed templates/wormhole.html.template wormholeTemplateString string - wormholeTemplate = template.Must(template.Must(outlineTemplate.Clone()).New("body").Parse(wormholeTemplateString)).Lookup("outline") + wormholeTemplate = template.Must(outlinedTemplate(wormholeTemplateString)) ) type wormholeTemplateArgs struct { @@ -441,7 +455,7 @@ func uploadWormhole(reader *multipart.Reader) (string, error) { var ( //go:embed templates/wormhole-success.html.template wormholeSuccessTemplateString string - wormholeSuccessTemplate = template.Must(template.Must(outlineTemplate.Clone()).New("body").Parse(wormholeSuccessTemplateString)).Lookup("outline") + wormholeSuccessTemplate = template.Must(outlinedTemplate(wormholeSuccessTemplateString)) ) type wormholeSuccessTemplateArgs struct { @@ -680,7 +694,7 @@ func checkPassword(password, auth string) bool { var ( //go:embed templates/login.html.template loginTemplateString string - loginTemplate = template.Must(template.Must(outlineTemplate.Clone()).New("body").Parse(loginTemplateString)).Lookup("outline") + loginTemplate = template.Must(outlinedTemplate(loginTemplateString)) ) type loginTemplateArgs struct { @@ -732,7 +746,7 @@ func login(w http.ResponseWriter, r *http.Request) { var ( //go:embed templates/note.html.template notepadString string - notepadTemplate = template.Must(template.Must(outlineTemplate.Clone()).New("body").Parse(notepadString)).Lookup("outline") + notepadTemplate = template.Must(outlinedTemplate(notepadString)) notepadContentsMu sync.Mutex notepadContents string -- cgit v1.3.1