From 006c0028106425abb3f718b2e86349dee5b7a2ea Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 26 May 2024 21:43:49 +0200 Subject: Refactor how preprocess functions work a bit --- src/http/http.go | 9 +++++---- src/http/posts.go | 57 ++++++++++++++++++++++++++++--------------------------- src/http/tpl.go | 2 +- 3 files changed, 35 insertions(+), 33 deletions(-) (limited to 'src/http') diff --git a/src/http/http.go b/src/http/http.go index 6e57a77..4403a69 100644 --- a/src/http/http.go +++ b/src/http/http.go @@ -126,9 +126,10 @@ type api struct { params Params srv *http.Server - redirectTpl *template.Template - auther Auther - urlBuilder render.URLBuilder + redirectTpl *template.Template + auther Auther + urlBuilder render.URLBuilder + postPreprocessFuncs postPreprocessFuncs } // New initializes and returns a new API instance, including setting up all @@ -156,12 +157,12 @@ func New(params Params) (API, error) { ), } + a.postPreprocessFuncs = newPostPreprocessFuncs(a.urlBuilder) a.redirectTpl = mustParseTpl(template.New(""), "redirect.html") a.srv = &http.Server{Handler: a.handler()} go func() { - err := a.srv.Serve(l) if err != nil && !errors.Is(err, http.ErrServerClosed) { ctx := mctx.WithAnnotator(context.Background(), &a.params) diff --git a/src/http/posts.go b/src/http/posts.go index 8488996..1bc65c8 100644 --- a/src/http/posts.go +++ b/src/http/posts.go @@ -5,10 +5,10 @@ import ( "context" "errors" "fmt" + "html/template" "net/http" "path/filepath" "strings" - txttpl "text/template" "time" "dev.mediocregopher.com/mediocre-blog.git/src/http/apiutil" @@ -18,7 +18,18 @@ import ( "dev.mediocregopher.com/mediocre-go-lib.git/mctx" ) -func (a *api) postPreprocessFuncImage(args ...string) (string, error) { +type postPreprocessFuncs struct { + urlBuilder render.URLBuilder + imageTpl *template.Template +} + +func newPostPreprocessFuncs(urlBuilder render.URLBuilder) postPreprocessFuncs { + imageTpl := template.New("image.html") + imageTpl = template.Must(imageTpl.Parse(mustReadTplFile("image.html"))) + return postPreprocessFuncs{urlBuilder, imageTpl} +} + +func (f postPreprocessFuncs) Image(args ...string) (string, error) { var ( id = args[0] @@ -29,39 +40,29 @@ func (a *api) postPreprocessFuncImage(args ...string) (string, error) { descr = args[1] } - tpl := txttpl.New("image.html") - tpl = txttpl.Must(tpl.Parse(mustReadTplFile("image.html"))) - - tplPayload := struct { - RootURL render.URLBuilder - ID string - Descr string - Resizable bool - }{ - RootURL: render.NewURLBuilder( - a.params.PublicURL, - a.params.PublicURL, // httpURL - a.params.GeminiPublicURL, - ), - ID: id, - Descr: descr, - Resizable: asset.IsImageResizable(id), - } + var ( + tplPayload = struct { + RootURL render.URLBuilder + ID string + Descr string + Resizable bool + }{ + RootURL: f.urlBuilder, + ID: id, + Descr: descr, + Resizable: asset.IsImageResizable(id), + } + buf = new(bytes.Buffer) + ) - buf := new(bytes.Buffer) - if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil { + err := f.imageTpl.ExecuteTemplate(buf, "image.html", tplPayload) + if err != nil { return "", err } return buf.String(), nil } -func (a *api) postPreprocessFuncs() post.PreprocessFunctions { - return post.PreprocessFunctions{ - Image: a.postPreprocessFuncImage, - } -} - func (a *api) getPostsHandler() http.Handler { var ( tpl = a.mustParseBasedTpl("posts.html") diff --git a/src/http/tpl.go b/src/http/tpl.go index 47cea80..42341a2 100644 --- a/src/http/tpl.go +++ b/src/http/tpl.go @@ -66,7 +66,7 @@ func (a *api) newTPLData(r *http.Request, payload interface{}) tplData { a.params.PostStore, a.params.PostAssetStore, a.params.PostDraftStore, - a.postPreprocessFuncs(), + a.postPreprocessFuncs, ), Payload: payload, Title: "mediocregopher's lil web corner", -- cgit v1.2.3